예제 #1
0
 def test_maximum_qubit(self):
     command_alloc0 = MagicMock(gate=Allocate,
                                qubits=[[MagicMock(id=0)],
                                        [MagicMock(id=1)]])
     command_alloc1 = MagicMock(gate=Allocate,
                                qubits=[[MagicMock(id=1)],
                                        [MagicMock(id=1)]])
     command_alloc2 = MagicMock(gate=Allocate,
                                qubits=[[MagicMock(id=2)],
                                        [MagicMock(id=1)]])
     command_dealloc0 = MagicMock(gate=Deallocate,
                                  qubits=[[MagicMock(id=0)],
                                          [MagicMock(id=1)]])
     command_dealloc1 = MagicMock(gate=Deallocate,
                                  qubits=[[MagicMock(id=1)],
                                          [MagicMock(id=1)]])
     command_dealloc2 = MagicMock(gate=Deallocate,
                                  qubits=[[MagicMock(id=2)],
                                          [MagicMock(id=1)]])
     command_list = [
         command_alloc1, command_alloc2, command_dealloc1, command_alloc0,
         command_dealloc0, command_dealloc2
     ]
     api = MockApiClient()
     backend = QIBackend(quantum_inspire_api=api)
     backend.main_engine = MagicMock()
     backend.receive(command_list)
     self.assertEqual(backend._number_of_qubits, 3)
     self.assertEqual(len(backend._allocated_qubits), 0)
예제 #2
0
 def test_logical_to_physical_raises_runtime_error(self):
     qd_id = 0
     expected = 1234
     api = MockApiClient()
     backend = QIBackend(quantum_inspire_api=api)
     backend.main_engine = MagicMock()
     backend.main_engine.mapper.current_mapping = [expected]
     self.assertRaises(RuntimeError, backend._logical_to_physical, qd_id)
예제 #3
0
 def test_logical_to_physical_without_mapper_returns_correct_result(self):
     qd_id = 1234
     expected = qd_id
     api = MockApiClient()
     backend = QIBackend(quantum_inspire_api=api)
     backend.main_engine = MagicMock(mapper=None)
     actual = backend._logical_to_physical(qd_id)
     self.assertEqual(actual, expected)
예제 #4
0
 def test_store_returns_correct_qasm_fsp_program_1(self):
     api = MockApiClient()
     backend = QIBackend(quantum_inspire_api=api)
     backend.main_engine = MagicMock(mapper=None)
     self.__store_function(backend, 0, Allocate)
     self.__store_function(backend, 1, Allocate)
     self.__store_function(backend, 0, H)
     self.__store_function(backend, 1, NOT, count=1)
     self.assertEqual(backend.qasm, "\nh q[0]\ncnot q[0], q[1]")
예제 #5
0
 def test_logical_to_physical_with_mapper_returns_correct_result(self):
     qd_id = 0
     expected = 1234
     api = MockApiClient()
     backend = QIBackend(quantum_inspire_api=api)
     backend.main_engine = MagicMock()
     backend.main_engine.mapper.current_mapping = [expected, qd_id]
     actual = backend._logical_to_physical(qd_id)
     self.assertEqual(actual, expected)
예제 #6
0
 def test_store_verbose_output(self):
     with patch('sys.stdout', new_callable=io.StringIO) as mock_stdout:
         api = MockApiClient()
         backend = QIBackend(quantum_inspire_api=api, verbose=3)
         backend.main_engine = MagicMock(mapper=None)
         self.__store_function(backend, 0, Allocate)
         self.assertEqual(backend.qasm, "")
         std_output = mock_stdout.getvalue()
     self.assertTrue('_store ' in std_output)
     self.assertTrue(': cmd ' in std_output)
예제 #7
0
 def test_receive(self, function_mock):
     function_mock.return_value = 1
     command = MagicMock(gate=NOT,
                         qubits=[[MagicMock(id=0)], [MagicMock(id=1)]])
     command_list = [command, MagicMock(gate=FlushGate())]
     api = MockApiClient()
     backend = QIBackend(quantum_inspire_api=api)
     backend.main_engine = MagicMock()
     with patch('sys.stdout', new_callable=io.StringIO):
         backend.receive(command_list)
     self.assertEqual(backend.qasm, "")
     self.assertTrue(backend._clear)
예제 #8
0
    def test_deallocation_of_unused_bits(self):
        command_alloc0 = MagicMock(gate=Allocate, qubits=[[MagicMock(id=0)]])
        command_alloc1 = MagicMock(gate=Allocate, qubits=[[MagicMock(id=1)]])
        command_dealloc0 = MagicMock(gate=Deallocate, qubits=[[MagicMock(id=0)]])
        command_dealloc2 = MagicMock(gate=Deallocate, qubits=[[MagicMock(id=2)]])

        command_list = [command_alloc0, command_alloc1, command_dealloc0, command_dealloc2]
        api = MockApiClient()
        backend = QIBackend(quantum_inspire_api=api)
        backend.main_engine = MagicMock()
        self.assertRaisesRegex(RuntimeError, "De-allocated bit 2 was not allocated.",
                               backend.receive, command_list)
예제 #9
0
 def test_reuse_after_flush_raises_runtime_error(self, function_mock):
     function_mock.return_value = 1
     command_alloc0 = MagicMock(gate=Allocate, qubits=[[MagicMock(id=0)]])
     command_alloc1 = MagicMock(gate=Allocate, qubits=[[MagicMock(id=1)]])
     command = MagicMock(gate=NOT, qubits=[[MagicMock(id=0)]], control_qubits=[MagicMock(id=1)])
     command_list = [command_alloc0, command_alloc1, command, MagicMock(gate=FlushGate()), command]
     api = MockApiClient()
     backend = QIBackend(quantum_inspire_api=api)
     backend.main_engine = MagicMock()
     with patch('sys.stdout', new_callable=io.StringIO):
         self.assertRaisesRegex(RuntimeError, "Same instance of QIBackend used for circuit after Flush.",
                                backend.receive, command_list)
예제 #10
0
 def test_store_measure_gate_without_mapper(self, function_mock):
     mock_tag = 'mock_my_tag'
     api = MockApiClient()
     function_mock.return_value = 4
     backend = QIBackend(quantum_inspire_api=api)
     command = MagicMock(gate=Measure,
                         qubits=[[MagicMock(id=mock_tag)]],
                         control_qubits=[MagicMock(id=2),
                                         MagicMock(id=3)],
                         tags=[])
     backend.main_engine = MagicMock(mapper=None)
     backend._store(command)
     self.assertEqual(backend._measured_ids, [mock_tag])
예제 #11
0
 def test_run_raises_error_no_result(self):
     api = MockApiClient()
     with patch('sys.stdout', new_callable=io.StringIO):
         backend = QIBackend(quantum_inspire_api=api, verbose=2)
         backend.qasm = "_"
         backend._measured_ids = [0, 1]
         backend.main_engine = MagicMock()
         backend.main_engine.mapper.current_mapping = [0, 1]
         result_mock = MagicMock()
         result_mock.get.return_value = {}
         api.execute_qasm.return_value = result_mock
         self.assertRaisesRegex(ProjectQBackendError, 'raw_text',
                                backend._run)
     api.execute_qasm.assert_called_once()
예제 #12
0
 def test_run_has_correct_output(self):
     api = MockApiClient()
     with patch('sys.stdout', new_callable=io.StringIO) as std_mock:
         backend = QIBackend(quantum_inspire_api=api, verbose=2)
         backend.qasm = "_"
         backend._measured_ids = [0]
         backend.main_engine = MagicMock()
         backend.main_engine.mapper.current_mapping = [0, 1]
         backend._run()
         std_output = std_mock.getvalue()
         actual = backend._quantum_inspire_result
     api.execute_qasm.assert_called_once()
     self.assertEqual(api.execute_qasm(), actual)
     self.assertTrue(backend._clear)
예제 #13
0
    def test_usage_of_non_allocate_qubit(self):
        command_alloc0 = MagicMock(gate=Allocate, qubits=[[MagicMock(id=0)]])
        command_alloc1 = MagicMock(gate=Allocate, qubits=[[MagicMock(id=1)]])
        command_dealloc0 = MagicMock(gate=Deallocate, qubits=[[MagicMock(id=0)]])

        command_h0 = MagicMock(gate=H, qubits=[[MagicMock(id=0)]], control_qubits=[])
        command_h1 = MagicMock(gate=H, qubits=[[MagicMock(id=1)]], control_qubits=[])

        command_list = [command_alloc0, command_alloc1, command_h1, command_dealloc0, command_h0]
        api = MockApiClient()
        backend = QIBackend(quantum_inspire_api=api)
        backend.main_engine = MagicMock()
        self.assertRaisesRegex(RuntimeError, "Bit position in simulation backend not found for physical bit 0.",
                               backend.receive, command_list)
예제 #14
0
 def test_get_probabilities_returns_correct_result(self):
     api = MockApiClient()
     backend = QIBackend(quantum_inspire_api=api)
     value_a = 0.4892578125
     value_b = 0.5097656250
     backend._measured_states = {
         0: value_a,
         11: value_b
     }  # 00000000 and 00001011
     expected = {'00': value_a, '11': value_b}
     backend.main_engine = MagicMock()
     backend._measured_ids = [0, 1]
     backend.main_engine.mapper.current_mapping = [0, 1]
     actual = backend.get_probabilities([MagicMock(id=0), MagicMock(id=1)])
     self.assertDictEqual(expected, actual)
예제 #15
0
 def test_flush_with_no_measurements_but_nfsp(self, function_mock):
     function_mock.return_value = 1
     command_alloc0 = MagicMock(gate=Allocate, qubits=[[MagicMock(id=0)]])
     command_alloc1 = MagicMock(gate=Allocate, qubits=[[MagicMock(id=1)]])
     command_alloc2 = MagicMock(gate=Allocate, qubits=[[MagicMock(id=1)]])
     command_dealloc1 = MagicMock(gate=Deallocate, qubits=[[MagicMock(id=1)]])
     command = MagicMock(gate=NOT, qubits=[[MagicMock(id=0)]], control_qubits=[MagicMock(id=1)])
     command_list = [command_alloc0, command_alloc1, command_dealloc1, command_alloc2, command,
                     MagicMock(gate=FlushGate())]
     api = MockApiClient()
     backend = QIBackend(quantum_inspire_api=api, verbose=1)
     backend.main_engine = MagicMock()
     with patch('sys.stdout', new_callable=io.StringIO):
         backend.receive(command_list)
     self.assertEqual(backend.qasm, "")