def test_cqasm_returns_correct_cqasm_data(self): api = MockApiClient() backend = QIBackend(quantum_inspire_api=api) expected = 'fake_cqasm_data' backend._cqasm = expected actual = backend.cqasm() self.assertEqual(actual, expected)
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)
def __store_function_assert_equal(self, identity, gate, qasm, function_mock, count=0, verbose=0): api = MockApiClient() function_mock.return_value = count backend = QIBackend(quantum_inspire_api=api, verbose=verbose) command_alloc0 = MagicMock(gate=Allocate, qubits=[[MagicMock(id=identity - 1)]]) command_alloc1 = MagicMock(gate=Allocate, qubits=[[MagicMock(id=identity)]]) command_alloc2 = MagicMock(gate=Allocate, qubits=[[MagicMock(id=identity + 1)]]) command = MagicMock(gate=gate, qubits=[[MagicMock(id=identity)], [MagicMock(id=identity + 1)]], control_qubits=[ MagicMock(id=identity - 1), MagicMock(id=identity) ]) command_list = [ command_alloc0, command_alloc1, command_alloc2, command ] backend.receive(command_list) self.assertEqual(backend.qasm, qasm)
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)
def __is_available_assert_equal(self, gate, expected, function_mock, count=0): command = MagicMock() command.gate = gate api = MockApiClient() function_mock.return_value = count backend = QIBackend(quantum_inspire_api=api) actual = backend.is_available(command) self.assertEqual(actual, expected, msg="{} failed!".format(gate))
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)
def test_is_available_verbose_prints_data(self): command = MagicMock() command.gate = CNOT api = MockApiClient() backend = QIBackend(quantum_inspire_api=api, verbose=3) with patch('sys.stdout', new_callable=io.StringIO) as mock_stdout: _ = backend.is_available(command) std_output = mock_stdout.getvalue() self.assertTrue(std_output.startswith('call to is_available with cmd'))
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]")
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)
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)
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)
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)
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)
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])
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()
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)
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, "")
def create_engine(use_QI_backend=False, api=None, num_runs=1, verbose=False, gate_fusion=False): """ Creates a new MainEngine. If use_QI_backend= True, Quantum Inspire is used as backend, else the ProjectQ default Simulator Returns engine for simulation Note: backend can be accessed via engine.backend both for QuantumInspire and ProjectQ simulators """ if use_QI_backend: assert ( api is not None), 'api must be defined if QI-backend should be used' # Set up compiler engines compiler_engines = default.get_engine_list() compiler_engines.extend([ManualMapper(lambda x: x)]) if use_QI_backend: qi_backend = QIBackend(quantum_inspire_api=api, num_runs=num_runs) engine = MainEngine(backend=qi_backend, engine_list=compiler_engines, verbose=verbose) else: sim = Simulator(gate_fusion=gate_fusion) engine = MainEngine(backend=sim, engine_list=compiler_engines, verbose=verbose) return engine
def __store_function_raises_error(self, gate, function_mock, count=0): identity = 1 api = MockApiClient() function_mock.return_value = count backend = QIBackend(quantum_inspire_api=api) command = [MagicMock(gate=gate, qubits=[[MagicMock(id=identity)], [MagicMock(id=identity + 1)]], control_qubits=[MagicMock(id=identity - 1), MagicMock(id=identity)])] self.assertRaises(NotImplementedError, backend.receive, command)
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)
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)
def test_init_has_correct_values(self): api = MockApiClient() backend = QIBackend(quantum_inspire_api=api) self.assertIsInstance(backend.qasm, str) self.assertEqual(backend.quantum_inspire_api, api) self.assertIsNone(backend.backend_type)
def test_run_no_qasm(self): api = MockApiClient() backend = QIBackend(quantum_inspire_api=api) backend._run() self.assertEqual(backend.qasm, "")
password = getpass() else: email, password = QI_EMAIL, QI_PASSWORD return get_basic_authentication(email, password) if __name__ == '__main__': name = 'TestProjectQ' authentication = get_authentication() qi_api = QuantumInspireAPI(QI_URL, authentication, project_name=name) compiler_engines = restrictedgateset.get_engine_list(one_qubit_gates="any", two_qubit_gates=(CNOT,)) compiler_engines.extend([ResourceCounter()]) qi_backend = QIBackend(quantum_inspire_api=qi_api) engine = MainEngine(backend=qi_backend, engine_list=compiler_engines) qubits = engine.allocate_qureg(2) q1 = qubits[0] q2 = qubits[1] H | q1 CNOT | (q1, q2) All(Measure) | qubits engine.flush() print('\nMeasured: {0}'.format([int(q) for q in qubits])) print('Probabilities {0}'.format(qi_backend.get_probabilities(qubits)))
def test_get_probabilities_raises_runtime_error(self): api = MockApiClient() backend = QIBackend(quantum_inspire_api=api) self.assertRaises(RuntimeError, backend.get_probabilities, None)
def test_reset_is_cleared(self): api = MockApiClient() backend = QIBackend(quantum_inspire_api=api) backend._clear = True backend._reset() self.assertTrue(backend._clear)
password = getpass() else: email, password = QI_EMAIL, QI_PASSWORD return get_basic_authentication(email, password) if __name__ == '__main__': # Remote Quantum-Inspire backend # authentication = get_authentication() qi = QuantumInspireAPI(r'https://api.quantum-inspire.com/', authentication) compiler_engines = restrictedgateset.get_engine_list( one_qubit_gates="any", two_qubit_gates=(CNOT, CZ, Toffoli)) compiler_engines.extend([ResourceCounter()]) qi_backend = QIBackend(quantum_inspire_api=qi) qi_engine = MainEngine(backend=qi_backend, engine_list=compiler_engines) # Run remote Grover search to find a n-bit solution result_qi = run_grover(qi_engine, 3, alternating_bits_oracle) print("\nResult from the remote Quantum-Inspire backend: {}".format( result_qi)) # Local ProjectQ simulator backend # compiler_engines = restrictedgateset.get_engine_list(one_qubit_gates="any", two_qubit_gates=(CNOT, CZ)) compiler_engines.append(ResourceCounter()) local_engine = MainEngine(Simulator(), compiler_engines) # Run local Grover search to find a n-bit solution
def get_engine(api=None): compiler_engines = restrictedgateset.get_engine_list(one_qubit_gates="any", two_qubit_gates=(CNOT, CZ, Toffoli)) compiler_engines.extend([ResourceCounter()]) qi_backend = QIBackend(quantum_inspire_api=api) return MainEngine(backend=qi_backend, engine_list=compiler_engines), qi_backend