|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import getpass |
| 3 | + |
| 4 | +from projectq import MainEngine |
| 5 | +from projectq.backends import AQTBackend |
| 6 | +from projectq.libs.hist import histogram |
| 7 | +from projectq.ops import Measure, Entangle, All |
| 8 | +import projectq.setups.aqt |
| 9 | + |
| 10 | + |
| 11 | +def run_entangle(eng, num_qubits=3): |
| 12 | + """ |
| 13 | + Runs an entangling operation on the provided compiler engine. |
| 14 | +
|
| 15 | + Args: |
| 16 | + eng (MainEngine): Main compiler engine to use. |
| 17 | + num_qubits (int): Number of qubits to entangle. |
| 18 | +
|
| 19 | + Returns: |
| 20 | + measurement (list<int>): List of measurement outcomes. |
| 21 | + """ |
| 22 | + # allocate the quantum register to entangle |
| 23 | + qureg = eng.allocate_qureg(num_qubits) |
| 24 | + |
| 25 | + # entangle the qureg |
| 26 | + Entangle | qureg |
| 27 | + |
| 28 | + # measure; should be all-0 or all-1 |
| 29 | + All(Measure) | qureg |
| 30 | + |
| 31 | + # run the circuit |
| 32 | + eng.flush() |
| 33 | + |
| 34 | + # access the probabilities via the back-end: |
| 35 | + # results = eng.backend.get_probabilities(qureg) |
| 36 | + # for state in results: |
| 37 | + # print("Measured {} with p = {}.".format(state, results[state])) |
| 38 | + # or plot them directly: |
| 39 | + histogram(eng.backend, qureg) |
| 40 | + plt.show() |
| 41 | + |
| 42 | + # return one (random) measurement outcome. |
| 43 | + return [int(q) for q in qureg] |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + #devices available to subscription: |
| 48 | + # aqt_simulator (11 qubits) |
| 49 | + # aqt_simulator_noise (11 qubits) |
| 50 | + # aqt_device (4 qubits) |
| 51 | + # |
| 52 | + # To get a subscription, create a profile at : |
| 53 | + # https://gateway-portal.aqt.eu/ |
| 54 | + # |
| 55 | + device = None # replace by the AQT device name you want to use |
| 56 | + token = None # replace by the token given by AQT |
| 57 | + if token is None: |
| 58 | + token = getpass.getpass(prompt='AQT token > ') |
| 59 | + if device is None: |
| 60 | + device = getpass.getpass(prompt='AQT device > ') |
| 61 | + # create main compiler engine for the AQT back-end |
| 62 | + eng = MainEngine(AQTBackend(use_hardware=True, token=token, num_runs=200, |
| 63 | + verbose=False, device=device), |
| 64 | + engine_list=projectq.setups.aqt.get_engine_list( |
| 65 | + token=token, device=device)) |
| 66 | + # run the circuit and print the result |
| 67 | + print(run_entangle(eng)) |
0 commit comments