def test_wrong_init(): with pytest.raises(TypeError): engine_list = restrictedgateset.get_engine_list(two_qubit_gates=(CNOT)) with pytest.raises(TypeError): engine_list = restrictedgateset.get_engine_list(one_qubit_gates="Any") with pytest.raises(TypeError): engine_list = restrictedgateset.get_engine_list(other_gates="any")
def test_state_preparation(n_qubits, zeros): engine_list = restrictedgateset.get_engine_list(one_qubit_gates=(Ry, Rz, Ph)) eng = projectq.MainEngine(engine_list=engine_list) qureg = eng.allocate_qureg(n_qubits) eng.flush() f_state = [ 0.2 + 0.1 * x * cmath.exp(0.1j + 0.2j * x) for x in range(2**n_qubits) ] if zeros: for i in range(2**(n_qubits - 1)): f_state[i] = 0 norm = 0 for amplitude in f_state: norm += abs(amplitude)**2 f_state = [x / math.sqrt(norm) for x in f_state] StatePreparation(f_state) | qureg eng.flush() wavefunction = deepcopy(eng.backend.cheat()[1]) # Test that simulator hasn't reordered wavefunction mapping = eng.backend.cheat()[0] for key in mapping: assert mapping[key] == key All(Measure) | qureg eng.flush() assert np.allclose(wavefunction, f_state, rtol=1e-10, atol=1e-10)
def get_engine_list(token=None, device=None): # Access to the hardware properties via show_devices # Can also be extended to take into account gate fidelities, new available # gate, etc.. devices = show_devices(token) aqt_setup = [] if device not in devices: raise DeviceOfflineError('Error when configuring engine list: device ' 'requested for Backend not connected') if device == 'aqt_simulator': # The 11 qubit online simulator doesn't need a specific mapping for # gates. Can also run wider gateset but this setup keep the # restrictedgateset setup for coherence mapper = BasicMapperEngine() # Note: Manual Mapper doesn't work, because its map is updated only if # gates are applied if gates in the register are not used, then it # will lead to state errors res = dict() for i in range(devices[device]['nq']): res[i] = i mapper.current_mapping = res aqt_setup = [mapper] else: # If there is an online device not handled into ProjectQ it's not too # bad, the engine_list can be constructed manually with the # appropriate mapper and the 'coupling_map' parameter raise DeviceNotHandledError('Device not yet fully handled by ProjectQ') # Most gates need to be decomposed into a subset that is manually converted # in the backend (until the implementation of the U1,U2,U3) setup = restrictedgateset.get_engine_list(one_qubit_gates=(Rx, Ry), two_qubit_gates=(Rxx, ), other_gates=(Barrier, )) setup.extend(aqt_setup) return setup
def get_engine_list(token=None, device=None): """Return the default list of compiler engine for the IonQ platform.""" service = IonQ() if token is not None: service.authenticate(token=token) devices = service.show_devices() if not device or device not in devices: raise DeviceOfflineError("Error checking engine list: no '{}' devices available".format(device)) # # Qubit mapper # mapper = BoundedQubitMapper(devices[device]['nq']) # # Basis Gates # # Declare the basis gateset for the IonQ's API. engine_list = restrictedgateset.get_engine_list( one_qubit_gates=(X, Y, Z, Rx, Ry, Rz, H, S, Sdag, T, Tdag, SqrtX), two_qubit_gates=(Swap, Rxx, Ryy, Rzz), other_gates=(Barrier,), ) return engine_list + [mapper]
def get_engine_list(credentials=None, device=None): """Return the default list of compiler engine for the AWS Braket platform.""" # Access to the hardware properties via show_devices # Can also be extended to take into account gate fidelities, new available # gate, etc.. devices = show_devices(credentials) if device not in devices: raise DeviceOfflineError( 'Error when configuring engine list: device requested for Backend not available' ) # We left the real device to manage the mapping and optimizacion: "The IonQ # and Rigetti devices compile the provided circuit into their respective # native gate sets automatically, and they map the abstract qubit indices # to physical qubits on the respective QPU." # (see: https://docs.aws.amazon.com/braket/latest/developerguide/braket-submit-to-qpu.html) # TODO: Investigate if explicit mapping is an advantage if device == 'SV1': setup = restrictedgateset.get_engine_list( one_qubit_gates=(R, H, Rx, Ry, Rz, S, Sdag, T, Tdag, X, Y, Z, SqrtX), two_qubit_gates=(Swap, ), other_gates=(Barrier, ), ) return setup if device == 'Aspen-8': setup = restrictedgateset.get_engine_list( one_qubit_gates=(R, H, Rx, Ry, Rz, S, Sdag, T, Tdag, X, Y, Z), two_qubit_gates=(Swap, ), other_gates=(Barrier, ), ) return setup if device == 'IonQ Device': setup = restrictedgateset.get_engine_list( one_qubit_gates=(H, Rx, Ry, Rz, S, Sdag, T, Tdag, X, Y, Z, SqrtX), two_qubit_gates=(Swap, ), other_gates=(Barrier, ), ) return setup raise DeviceNotHandledError( 'Unsupported device type: {}!'.format(device)) # pragma: no cover
def get_engine_list(): # lets start from a circuit that has CNOT, Pauli, S and T and time evolution engines = restrictedgateset.get_engine_list( one_qubit_gates=(gates.HGate, gates.XGate, gates.YGate, gates.ZGate, gates.TGate, gates.Tdag, gates.SGate, gates.Sdag), two_qubit_gates=(gates.CNOT, ), other_gates=(gates.TimeEvolution, )) engines = engines + [ PermutePi4Front(), MultiqubitMeasurementCliffordEngine() ] return engines
def get_engine_list(): """ Creates a list of engines that compile to QSCOUT's native gate set. :returns: The engines. :rtype: list(projectq.cengines.BasicEngine) """ return restrictedgateset.get_engine_list( one_qubit_gates=tuple(one_qubit_gates.keys()), two_qubit_gates=tuple(two_qubit_gates.keys()), other_gates=(BarrierGate, ), compiler_chooser=trapped_ion_decomposer.chooser_Ry_reducer, )
def test_parameter_any(): engine_list = restrictedgateset.get_engine_list(one_qubit_gates="any", two_qubit_gates="any") backend = DummyEngine(save_commands=True) eng = projectq.MainEngine(backend, engine_list) qubit1 = eng.allocate_qubit() qubit2 = eng.allocate_qubit() gate = BasicGate() gate | (qubit1, qubit2) gate | qubit1 eng.flush() print(len(backend.received_commands)) assert backend.received_commands[2].gate == gate assert backend.received_commands[3].gate == gate
def OpenSurgeryExporterEngineList(): # lets start from a circuit that has CNOT, Pauli, S and T and time evolution engines = restrictedgateset.get_engine_list( one_qubit_gates=(gates.HGate, gates.XGate, gates.YGate, gates.ZGate, gates.TGate, gates.Tdag, gates.SGate, gates.Sdag), two_qubit_gates=(gates.CNOT, ), other_gates=()) engines = expand_rule_set_of_autoreplacer(engines) + [ PermutePi4Front(), MultiqubitMeasurementCliffordEngine(), BasisRotation() ] return engines
def test_restriction(): engine_list = restrictedgateset.get_engine_list( one_qubit_gates=(Rz, H), two_qubit_gates=(CNOT, AddConstant, Swap), other_gates=(Toffoli, AddConstantModN, MultiplyByConstantModN(2, 8)), ) backend = DummyEngine(save_commands=True) eng = projectq.MainEngine(backend, engine_list, verbose=True) qubit1 = eng.allocate_qubit() qubit2 = eng.allocate_qubit() qubit3 = eng.allocate_qubit() eng.flush() CNOT | (qubit1, qubit2) H | qubit1 with Control(eng, qubit2): Rz(0.2) | qubit1 Measure | qubit1 AddConstant(1) | (qubit1 + qubit2) AddConstantModN(1, 9) | (qubit1 + qubit2 + qubit3) Toffoli | (qubit1 + qubit2, qubit3) Swap | (qubit1, qubit2) MultiplyByConstantModN(2, 8) | qubit1 + qubit2 + qubit3 TimeEvolution(0.5, QubitOperator("X0 Y1 Z2")) | qubit1 + qubit2 + qubit3 QFT | qubit1 + qubit2 + qubit3 Rx(0.1) | (qubit1) MultiplyByConstantModN(2, 9) | qubit1 + qubit2 + qubit3 eng.flush() assert backend.received_commands[4].gate == X assert len(backend.received_commands[4].control_qubits) == 1 assert backend.received_commands[5].gate == H assert backend.received_commands[6].gate == Rz(0.1) assert backend.received_commands[10].gate == Measure assert backend.received_commands[11].gate == AddConstant(1) assert backend.received_commands[12].gate == AddConstantModN(1, 9) assert backend.received_commands[13].gate == X assert len(backend.received_commands[13].control_qubits) == 2 assert backend.received_commands[14].gate == Swap assert backend.received_commands[15].gate == MultiplyByConstantModN(2, 8) for cmd in backend.received_commands[16:]: assert cmd.gate != QFT assert not isinstance(cmd.gate, Rx) assert not isinstance(cmd.gate, MultiplyByConstantModN) assert not isinstance(cmd.gate, TimeEvolution)
def get_engine_list(): """ Returns an engine list compiling code into a trapped ion based compiled circuit code. Note: - Classical instructions gates such as e.g. Flush and Measure are automatically allowed. - The restricted gate set engine does not work with Rxx gates, as ProjectQ will by default bounce back and forth between Cz gates and Cx gates. An appropriate decomposition chooser needs to be used! Returns: A list of suitable compiler engines. """ return restrictedgateset.get_engine_list( one_qubit_gates=(Rx, Ry), two_qubit_gates=(Rxx, ), compiler_chooser=chooser_Ry_reducer)
from projectq.ops import CNOT, H, Measure, All from projectq.setups import restrictedgateset from quantuminspire.api import QuantumInspireAPI from quantuminspire.credentials import get_authentication from quantuminspire.projectq.backend_qx import QIBackend QI_URL = os.getenv('API_URL', 'https://api.quantum-inspire.com/') project_name = 'ProjectQ-entangle' authentication = get_authentication() qi_api = QuantumInspireAPI(QI_URL, authentication, project_name=project_name) qi_backend = QIBackend(quantum_inspire_api=qi_api) compiler_engines = restrictedgateset.get_engine_list( one_qubit_gates=qi_backend.one_qubit_gates, two_qubit_gates=qi_backend.two_qubit_gates) compiler_engines.extend([ResourceCounter()]) 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]))
def test_ibm_backend_functional_test(monkeypatch): correct_info = { 'json': [{ 'qubits': [1], 'name': 'u2', 'params': [0, 3.141592653589793] }, { 'qubits': [1, 2], 'name': 'cx' }, { 'qubits': [1, 3], 'name': 'cx' }, { 'qubits': [1], 'name': 'u3', 'params': [6.28318530718, 0, 0] }, { 'qubits': [1], 'name': 'u1', 'params': [11.780972450962] }, { 'qubits': [1], 'name': 'u3', 'params': [6.28318530718, 0, 0] }, { 'qubits': [1], 'name': 'u1', 'params': [10.995574287564] }, { 'qubits': [1, 2, 3], 'name': 'barrier' }, { 'qubits': [1], 'name': 'u3', 'params': [0.2, -1.5707963267948966, 1.5707963267948966] }, { 'qubits': [1], 'name': 'measure', 'memory': [1] }, { 'qubits': [2], 'name': 'measure', 'memory': [2] }, { 'qubits': [3], 'name': 'measure', 'memory': [3] }], 'nq': 4, 'shots': 1000, 'maxCredits': 10, 'backend': { 'name': 'ibmq_qasm_simulator' } } # {'qasms': [{'qasm': '\ninclude "qelib1.inc";\nqreg q[4];\ncreg c[4];\nu2(0,pi/2) q[1];\ncx q[1], q[2];\ncx q[1], q[3];\nu3(6.28318530718, 0, 0) q[1];\nu1(11.780972450962) q[1];\nu3(6.28318530718, 0, 0) q[1];\nu1(10.995574287564) q[1];\nu3(0.2, -pi/2, pi/2) q[1];\nmeasure q[1] -> c[1];\nmeasure q[2] -> c[2];\nmeasure q[3] -> c[3];'}], 'json': [{'qubits': [1], 'name': 'u2', 'params': [0, 3.141592653589793]}, {'qubits': [1, 2], 'name': 'cx'}, {'qubits': [1, 3], 'name': 'cx'}, {'qubits': [1], 'name': 'u3', 'params': [6.28318530718, 0, 0]}, {'qubits': [1], 'name': 'u1', 'params': [11.780972450962]}, {'qubits': [1], 'name': 'u3', 'params': [6.28318530718, 0, 0]}, {'qubits': [1], 'name': 'u1', 'params': [10.995574287564]}, {'qubits': [1], 'name': 'u3', 'params': [0.2, -1.5707963267948966, 1.5707963267948966]}, {'qubits': [1], 'name': 'measure', 'memory': [1]}, {'qubits': [2], 'name': 'measure', 'memory': [2]}, {'qubits': [3], 'name': 'measure', 'memory': [3]}], 'nq': 4, 'shots': 1000, 'maxCredits': 10, 'backend': {'name': 'ibmq_qasm_simulator'}} def mock_send(*args, **kwargs): assert args[0] == correct_info return { 'data': { 'counts': { '0x0': 504, '0x2': 8, '0xc': 6, '0xe': 482 } }, 'header': { 'clbit_labels': [['c', 0], ['c', 1], ['c', 2], ['c', 3]], 'creg_sizes': [['c', 4]], 'memory_slots': 4, 'n_qubits': 32, 'name': 'circuit0', 'qreg_sizes': [['q', 32]], 'qubit_labels': [['q', 0], ['q', 1], ['q', 2], ['q', 3], ['q', 4], ['q', 5], ['q', 6], ['q', 7], ['q', 8], ['q', 9], ['q', 10], ['q', 11], ['q', 12], ['q', 13], ['q', 14], ['q', 15], ['q', 16], ['q', 17], ['q', 18], ['q', 19], ['q', 20], ['q', 21], ['q', 22], ['q', 23], ['q', 24], ['q', 25], ['q', 26], ['q', 27], ['q', 28], ['q', 29], ['q', 30], ['q', 31]] }, 'metadata': { 'measure_sampling': True, 'method': 'statevector', 'parallel_shots': 1, 'parallel_state_update': 16 }, 'seed_simulator': 465435780, 'shots': 1000, 'status': 'DONE', 'success': True, 'time_taken': 0.0045786460000000005 } monkeypatch.setattr(_ibm, "send", mock_send) backend = _ibm.IBMBackend(verbose=True, num_runs=1000) import sys # no circuit has been executed -> raises exception with pytest.raises(RuntimeError): backend.get_probabilities([]) mapper = BasicMapperEngine() res = dict() for i in range(4): res[i] = i mapper.current_mapping = res ibm_setup = [mapper] setup = restrictedgateset.get_engine_list(one_qubit_gates=(Rx, Ry, Rz, H), two_qubit_gates=(CNOT, ), other_gates=(Barrier, )) setup.extend(ibm_setup) eng = MainEngine(backend=backend, engine_list=setup) # 4 qubits circuit is run, but first is unused to test ability for # get_probability to return the correct values for a subset of the total # register unused_qubit = eng.allocate_qubit() qureg = eng.allocate_qureg(3) # entangle the qureg Entangle | qureg Tdag | qureg[0] Sdag | qureg[0] Barrier | qureg Rx(0.2) | qureg[0] del unused_qubit # measure; should be all-0 or all-1 All(Measure) | qureg # run the circuit eng.flush() prob_dict = eng.backend.get_probabilities([qureg[2], qureg[1]]) assert prob_dict['00'] == pytest.approx(0.512) assert prob_dict['11'] == pytest.approx(0.488) result = "\nu2(0,pi/2) q[1];\ncx q[1], q[2];\ncx q[1], q[3];" if sys.version_info.major == 3: result += "\nu3(6.28318530718, 0, 0) q[1];\nu1(11.780972450962) q[1];" result += "\nu3(6.28318530718, 0, 0) q[1];\nu1(10.995574287564) q[1];" else: result += "\nu3(6.28318530718, 0, 0) q[1];\nu1(11.780972451) q[1];" result += "\nu3(6.28318530718, 0, 0) q[1];\nu1(10.9955742876) q[1];" result += "\nbarrier q[1], q[2], q[3];" result += "\nu3(0.2, -pi/2, pi/2) q[1];\nmeasure q[1] -> c[1];" result += "\nmeasure q[2] -> c[2];\nmeasure q[3] -> c[3];" assert eng.backend.get_qasm() == result with pytest.raises(RuntimeError): eng.backend.get_probabilities(eng.allocate_qubit())
def test_ibm_retrieve(monkeypatch): # patch send def mock_retrieve(*args, **kwargs): return { 'data': { 'counts': { '0x0': 504, '0x2': 8, '0xc': 6, '0xe': 482 } }, 'header': { 'clbit_labels': [['c', 0], ['c', 1], ['c', 2], ['c', 3]], 'creg_sizes': [['c', 4]], 'memory_slots': 4, 'n_qubits': 32, 'name': 'circuit0', 'qreg_sizes': [['q', 32]], 'qubit_labels': [['q', 0], ['q', 1], ['q', 2], ['q', 3], ['q', 4], ['q', 5], ['q', 6], ['q', 7], ['q', 8], ['q', 9], ['q', 10], ['q', 11], ['q', 12], ['q', 13], ['q', 14], ['q', 15], ['q', 16], ['q', 17], ['q', 18], ['q', 19], ['q', 20], ['q', 21], ['q', 22], ['q', 23], ['q', 24], ['q', 25], ['q', 26], ['q', 27], ['q', 28], ['q', 29], ['q', 30], ['q', 31]] }, 'metadata': { 'measure_sampling': True, 'method': 'statevector', 'parallel_shots': 1, 'parallel_state_update': 16 }, 'seed_simulator': 465435780, 'shots': 1000, 'status': 'DONE', 'success': True, 'time_taken': 0.0045786460000000005 } monkeypatch.setattr(_ibm, "retrieve", mock_retrieve) backend = _ibm.IBMBackend(retrieve_execution="ab1s2", num_runs=1000) mapper = BasicMapperEngine() res = dict() for i in range(4): res[i] = i mapper.current_mapping = res ibm_setup = [mapper] setup = restrictedgateset.get_engine_list(one_qubit_gates=(Rx, Ry, Rz, H), two_qubit_gates=(CNOT, )) setup.extend(ibm_setup) eng = MainEngine(backend=backend, engine_list=setup) unused_qubit = eng.allocate_qubit() qureg = eng.allocate_qureg(3) # entangle the qureg Entangle | qureg Tdag | qureg[0] Sdag | qureg[0] Barrier | qureg Rx(0.2) | qureg[0] del unused_qubit # measure; should be all-0 or all-1 All(Measure) | qureg # run the circuit eng.flush() prob_dict = eng.backend.get_probabilities([qureg[0], qureg[2], qureg[1]]) assert prob_dict['000'] == pytest.approx(0.504) assert prob_dict['111'] == pytest.approx(0.482) assert prob_dict['011'] == pytest.approx(0.006)
def get_engine_list(token=None, device=None): """Return the default list of compiler engine for the IBM QE platform.""" # Access to the hardware properties via show_devices # Can also be extended to take into account gate fidelities, new available # gate, etc.. devices = show_devices(token) ibm_setup = [] if device not in devices: raise DeviceOfflineError('Error when configuring engine list: device requested for Backend not connected') if devices[device]['nq'] == 5: # The requested device is a 5 qubit processor # Obtain the coupling map specific to the device coupling_map = devices[device]['coupling_map'] coupling_map = list2set(coupling_map) mapper = IBM5QubitMapper(coupling_map) ibm_setup = [mapper, SwapAndCNOTFlipper(coupling_map), LocalOptimizer(10)] elif device == 'ibmq_qasm_simulator': # The 32 qubit online simulator doesn't need a specific mapping for # gates. Can also run wider gateset but this setup keep the # restrictedgateset setup for coherence mapper = BasicMapperEngine() # Note: Manual Mapper doesn't work, because its map is updated only if # gates are applied if gates in the register are not used, then it # will lead to state errors res = {} for i in range(devices[device]['nq']): res[i] = i mapper.current_mapping = res ibm_setup = [mapper] elif device == 'ibmq_16_melbourne': # Only 15 qubits available on this ibmqx2 unit(in particular qubit 7 # on the grid), therefore need custom grid mapping grid_to_physical = { 0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 15, 8: 14, 9: 13, 10: 12, 11: 11, 12: 10, 13: 9, 14: 8, 15: 7, } coupling_map = devices[device]['coupling_map'] coupling_map = list2set(coupling_map) ibm_setup = [ GridMapper(2, 8, grid_to_physical), LocalOptimizer(5), SwapAndCNOTFlipper(coupling_map), LocalOptimizer(5), ] else: # If there is an online device not handled into ProjectQ it's not too # bad, the engine_list can be constructed manually with the # appropriate mapper and the 'coupling_map' parameter raise DeviceNotHandledError('Device not yet fully handled by ProjectQ') # Most IBM devices accept U1,U2,U3,CX gates. # Most gates need to be decomposed into a subset that is manually converted # in the backend (until the implementation of the U1,U2,U3) # available gates decomposable now for U1,U2,U3: Rx,Ry,Rz and H setup = restrictedgateset.get_engine_list( one_qubit_gates=(Rx, Ry, Rz, H), two_qubit_gates=(CNOT,), other_gates=(Barrier,) ) setup.extend(ibm_setup) return setup
backend = SimulatorMPI(gate_fusion=True, num_local_qubits=2) cache_depth = 10 rule_set = DecompositionRuleSet(modules=[projectq.setups.decompositions]) temp = LocalOptimizer(cache_depth) engines = [ TagRemover(), temp, AutoReplacer(rule_set), TagRemover(), LocalOptimizer(cache_depth), GreedyScheduler() ] # create a list of restriction engines restric_engine = restrictedgateset.get_engine_list(one_qubit_gates=(X, Y, Z, H, S, T, Rx, Ry, Rz), two_qubit_gates=(CZ, CX)) eng = HiQMainEngine(backend, engine_list=engines + [CommandPrinter()]) class SqrtYGate(BasicGate): """ Square-root X gate class """ @property def matrix(self): return (0.5 + 0.5j) * np.matrix([[1, -1], [1, 1]]) def tex_str(self): return r'$\sqrt{Y}$' def __str__(self):
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
output (Qubit): Output qubit to flip in order to mark the solution. """ with Compute(eng): All(X) | qubits[1::2] with Control(eng, qubits): X | output Uncompute(eng) # Remote Quantum-Inspire backend authentication = get_authentication() qi = QuantumInspireAPI(QI_URL, authentication) qi_backend = QIBackend(quantum_inspire_api=qi) compiler_engines = restrictedgateset.get_engine_list( one_qubit_gates=qi_backend.one_qubit_gates, two_qubit_gates=qi_backend.two_qubit_gates, other_gates=qi_backend.three_qubit_gates) compiler_engines.extend([ResourceCounter()]) 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)
print('Enter email:') email = input() print('Enter password') 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)
#!/usr/bin/env python3 from projectq import MainEngine from projectq.backends import CommandPrinter, Simulator from projectq.setups import restrictedgateset from projectq.ops import X, Z, H, Ry, Rz, CNOT, All, Measure # set restricted gate-set restricted_list = restrictedgateset.get_engine_list(one_qubit_gates=(Rz, Ry), two_qubit_gates=(CNOT, ), other_gates=()) # set engine restricted_compiler = MainEngine(backend=CommandPrinter(accept_input=False), engine_list=restricted_list) # print qasm printqasm = MainEngine(backend=CommandPrinter(accept_input=False)) # specify simulate locally simulate = MainEngine(backend=Simulator()) def qprogram(eng): q = eng.allocate_qureg(8) # Q in binary: '01010001' # recall H Z H is X H | q[1] Z | q[1] H | q[1] X | q[3] X | q[7]