Execution of the Bell circuit
The “Hello World!” of Quantum Computing is the generation of the 2-qubit Bell state.
Creating the EPR/Bell state
Let us start with the circuit:
[1]:
from mpqp import QCircuit, H, CNOT
from mpqp.tools import pprint
[2]:
circuit = QCircuit([H(0), CNOT(0,1)], label="Bell state")
print(circuit)
┌───┐
q_0: ┤ H ├──■──
└───┘┌─┴─┐
q_1: ─────┤ X ├
└───┘
Run the circuit on a local device
We can execute a circuit on a local simulator by calling the function run and precising the device.
[3]:
from mpqp.execution import run, ATOSDevice
When no measure is added to the circuit, running the circuit will consist in extracting the state-vector at the output of the circuit.
⚠ This feature is not supported on all backends
[4]:
result = run(circuit, ATOSDevice.MYQLM_PYLINALG)
print(result)
pprint(result.amplitudes)
pprint(result.probabilities)
using legacy validation callback
Result: Bell state, ATOSDevice, MYQLM_PYLINALG
State vector: [0.70711, 0, 0, 0.70711]
Probabilities: [0.5, 0, 0, 0.5]
Number of qubits: 2
[0.70711, 0, 0, 0.70711]
[0.5, 0, 0, 0.5]
We can also add to the circuit a BasisMeasure, consisting in sample the state in a given basis. By default, the basis is the computational one.
[5]:
from mpqp import BasisMeasure
We precise which qubits we can to measure by inputting a list of indices, and precising the number of shots. When shots=0, we end up in the same case as before, a statevector simulation.
[6]:
circuit.add(BasisMeasure([0,1], shots=0))
result = run(circuit, ATOSDevice.MYQLM_PYLINALG)
print(result)
Result: Bell state, ATOSDevice, MYQLM_PYLINALG
State vector: [0.70711, 0, 0, 0.70711]
Probabilities: [0.5, 0, 0, 0.5]
Number of qubits: 2
When we precise a number of shots, the circuit will be sampled and the core of the Result will be a list of Sample. A precising the counts for each state of the basis.
[7]:
circuit = circuit.without_measurements()
circuit.add(BasisMeasure([0,1], shots=1024))
[8]:
result = run(circuit, ATOSDevice.MYQLM_PYLINALG)
print(result)
print(result.samples)
print(result.counts)
pprint(result.probabilities)
Result: Bell state, ATOSDevice, MYQLM_PYLINALG
Counts: [535, 0, 0, 489]
Probabilities: [0.52246, 0, 0, 0.47754]
Samples:
State: 00, Index: 0, Count: 535, Probability: 0.5224609
State: 11, Index: 3, Count: 489, Probability: 0.4775391
Error: 0.015616853889383952
[Sample(2, index=0, count=535, probability=0.5224609375), Sample(2, index=3, count=489, probability=0.4775390625)]
[535, 0, 0, 489]
[0.52246, 0, 0, 0.47754]
Run the circuit on multiple devices
By using the same function run we can execute the circuit on several simulators at the time. One just has to give a list of devices instead of a single device.
[9]:
from mpqp import IBMDevice, AWSDevice, GOOGLEDevice
[10]:
results = run(circuit, [ATOSDevice.MYQLM_PYLINALG, IBMDevice.AER_SIMULATOR, AWSDevice.BRAKET_LOCAL_SIMULATOR, GOOGLEDevice.CIRQ_LOCAL_SIMULATOR])
print(results)
c:\Users\HenrideBoutray\anaconda3\Lib\site-packages\mpqp\qasm\qasm_to_braket.py:134: UnsupportedBraketFeaturesWarning:
This program uses OpenQASM language features that may not be supported on QPUs or on-demand simulators.
warnings.warn(
BatchResult: 4 results
Result: Bell state, ATOSDevice, MYQLM_PYLINALG
Counts: [494, 0, 0, 530]
Probabilities: [0.48242, 0, 0, 0.51758]
Samples:
State: 00, Index: 0, Count: 494, Probability: 0.4824219
State: 11, Index: 3, Count: 530, Probability: 0.5175781
Error: 0.015622971329386458
Result: Bell state, IBMDevice, AER_SIMULATOR
Counts: [502, 0, 0, 522]
Probabilities: [0.49023, 0, 0, 0.50977]
Samples:
State: 00, Index: 0, Count: 502, Probability: 0.4902344
State: 11, Index: 3, Count: 522, Probability: 0.5097656
Error: None
Result: Bell state, AWSDevice, BRAKET_LOCAL_SIMULATOR
Counts: [508, 0, 0, 516]
Probabilities: [0.49609, 0, 0, 0.50391]
Samples:
State: 00, Index: 0, Count: 508, Probability: 0.4960938
State: 11, Index: 3, Count: 516, Probability: 0.5039062
Error: None
Result: Bell state, GOOGLEDevice, CIRQ_LOCAL_SIMULATOR
Counts: [501, 0, 0, 523]
Probabilities: [0.48926, 0, 0, 0.51074]
Samples:
State: 00, Index: 0, Count: 501, Probability: 0.4892578
State: 11, Index: 3, Count: 523, Probability: 0.5107422
Error: None
[11]:
print(results[0])
Result: Bell state, ATOSDevice, MYQLM_PYLINALG
Counts: [494, 0, 0, 530]
Probabilities: [0.48242, 0, 0, 0.51758]
Samples:
State: 00, Index: 0, Count: 494, Probability: 0.4824219
State: 11, Index: 3, Count: 530, Probability: 0.5175781
Error: 0.015622971329386458
Run or submit the circuit on a remote device
To execute the circuit on remote device, one can use the exact same process as with local devices. A call of the function run on a remote device will launch the job and wait until it finished before returning the result. One or several devices can still be given in parameter.
[12]:
result = run(circuit, IBMDevice.IBM_LEAST_BUSY)
print(result)
Result: Bell state, IBMDevice, IBM_KYIV
Counts: [489, 13, 11, 511]
Probabilities: [0.47754, 0.0127, 0.01074, 0.49902]
Samples:
State: 00, Index: 0, Count: 489, Probability: 0.4775391
State: 01, Index: 1, Count: 13, Probability: 0.0126953
State: 10, Index: 2, Count: 11, Probability: 0.0107422
State: 11, Index: 3, Count: 511, Probability: 0.4990234
Error: None
However, it is also possible to asynchronously submit the job way using the submit function.
[13]:
from mpqp import submit
By submitting the circuit to a remote device, we retrieve the id of the job attributed by the provider, as well as the corresponding MPQP job.
The MPQP job object contains additional information, such as the status of the job.
[14]:
job_id, job = submit(circuit, IBMDevice.IBM_LEAST_BUSY)
print(job_id)
czpnk3mkzhn0008bpnsg
Once the computation is done, we use the function get_remote_result for retrieving the result.
If the job is not completed, the function will wait (blocking) until it is done.
[15]:
from mpqp import get_remote_result
[16]:
result = get_remote_result(job_id, IBMDevice.IBM_LEAST_BUSY)
print(result)
Result: IBMDevice, IBM_KYIV
Counts: [485, 2, 10, 527]
Probabilities: [0.47363, 0.00195, 0.00977, 0.51465]
Samples:
State: 00, Index: 0, Count: 485, Probability: 0.4736328
State: 01, Index: 1, Count: 2, Probability: 0.0019531
State: 10, Index: 2, Count: 10, Probability: 0.0097656
State: 11, Index: 3, Count: 527, Probability: 0.5146484
Error: None