Created
June 25, 2018 08:41
-
-
Save nelimee/465638ef52540d0e984b7d93a5fbf78e to your computer and use it in GitHub Desktop.
A quantum program to validate the provided errors with an experiment on the real backend.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Qconfig | |
import qiskit | |
import numpy as np | |
qiskit.register(Qconfig.APItoken, Qconfig.config["url"]) | |
ibmqx4 = qiskit.get_backend('ibmqx4') | |
qubit_number = ibmqx4.configuration['n_qubits'] | |
shot_number = 8192 | |
## 1. Computing experimental probabilities. | |
Q_SPECS = { | |
"name": "Measurement_errors", | |
"circuits": [ | |
{ | |
"name": "measurement", | |
"quantum_registers": [ | |
{ | |
"name": "q", | |
"size": qubit_number | |
}, | |
], | |
"classical_registers": [ | |
{ | |
"name": "c", | |
"size": qubit_number | |
} | |
] | |
} | |
], | |
} | |
Q_program = qiskit.QuantumProgram(specs=Q_SPECS) | |
circuit = Q_program.get_circuit("measurement") | |
q = Q_program.get_quantum_register("q") | |
c = Q_program.get_classical_register("c") | |
circuit.measure(q, c) | |
print("Executing...") | |
result = qiskit.execute(circuit, ibmqx4, shots=shot_number, max_credits=5).result() | |
print("Done!") | |
counts = result.get_counts('measurement') | |
experimental_probabilities = np.array([counts.get(bin(n)[2:].zfill(qubit_number), 0) / shot_number | |
for n in range(2**qubit_number)]) | |
## 2. Computing theoretical probabilities | |
readout_errors = np.array([qubit_data['readoutError']['value'] for qubit_data in ibmqx4.calibration['qubits']]) | |
N = np.arange(2**qubit_number) | |
theoretical_probabilities = [] | |
for n in N: | |
mask = np.array([((n & (1<<i)) != 0) for i in range(5)]) | |
theoretical_probabilities.append(np.prod(readout_errors[mask]) * np.prod(1 - readout_errors[np.logical_not(mask)])) | |
theoretical_probabilities = np.array(theoretical_probabilities) | |
## 3. Printing the error probabilities | |
print("Experimental errors:\n{}".format(experimental_probabilities)) | |
print("Theoretical errors:\n{}".format(theoretical_probabilities)) | |
print("Error on errors: {}".format(np.linalg.norm(experimental_probabilities - theoretical_probabilities))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment