Skip to content

Instantly share code, notes, and snippets.

@marekyggdrasil
Last active November 13, 2019 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marekyggdrasil/bfcf9a9f3eac0f23c38142fec84156a6 to your computer and use it in GitHub Desktop.
Save marekyggdrasil/bfcf9a9f3eac0f23c38142fec84156a6 to your computer and use it in GitHub Desktop.
Quantum random generation implemented with IBM Q's AER results with non-even distribution even without noise model. Opened issue is: https://github.com/Qiskit/qiskit-aer/issues/437
Qiskit local environment
{ 'qiskit': '0.13.0',
'qiskit-aer': '0.3.2',
'qiskit-aqua': '0.6.1',
'qiskit-ibmq-provider': '0.3.3',
'qiskit-ignis': '0.2.0',
'qiskit-terra': '0.10.0'}
Is the new noise model ideal in terms of readout errors?
True
import numpy as np
import qiskit
from qiskit.tools.visualization import plot_histogram
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, Aer
from qiskit.providers.aer.noise import NoiseModel
from qiskit.providers.aer.noise.errors import ReadoutError
from qutip import basis, snot
backend_aer = Aer.get_backend("qasm_simulator")
import pprint
pp = pprint.PrettyPrinter(indent=4)
print("Qiskit local environment")
pp.pprint(qiskit.__qiskit_version__)
# setup simplest circuit that reproduces the issue
N = 1
shots = 1024
q = QuantumRegister(N)
c = ClassicalRegister(N)
qc = QuantumCircuit(q, c)
qc.h(q[0])
qc.measure(q, c)
diagram = qc.draw(output="mpl")
diagram.savefig("res_circuit.png", format="png")
# set the noise model to be None
nm = None
# setup the backend
job = qiskit.execute(qc, backend_aer, shots=shots, noise_model=nm)
result = job.result()
counts = result.get_counts()
title = "Noise model set to None"
figure = plot_histogram(counts, title=title)
figure.savefig("res_counts.png", format="png")
# create a new noise model that ensures no readout errors
nm = NoiseModel()
probabilities = []
for i in range(2**N):
lst = np.zeros(2**N)
lst[i] = 1.0
probabilities.append(lst)
error = ReadoutError(probabilities)
nm.add_all_qubit_readout_error(error)
print("\nIs the new noise model ideal in terms of readout errors?")
pp.pprint(error.ideal())
# run the backend again with no-readout-error noise model
job = qiskit.execute(qc, backend_aer, shots=shots, noise_model=nm)
result = job.result()
counts = result.get_counts()
title = "Ensured lack of readout errors"
figure = plot_histogram(counts, title=title)
figure.savefig("res_readout_counts.png", format="png")
# not let us perform equivalent calculation using QuTip
psi0 = basis(2, 0)
psif = snot()*psi0
P0 = np.power(np.abs(psif.overlap(basis(2, 0))), 2.0)
P1 = np.power(np.abs(psif.overlap(basis(2, 1))), 2.0)
qutip_counts = {
"0": int(P0*shots),
"1": int(P1*shots)
}
title = "Expected counts (QuTip)"
figure = plot_histogram(qutip_counts, title=title)
figure.savefig("res_qutip_counts.png", format="png")
@marekyggdrasil
Copy link
Author

resolved in Qiskit/qiskit-aer#437 (comment), quoting the answer from @nonhermitian

Measurements are random samples of the probability distribution that is 50/50. You should not expect to get exactly 50/50, just like you should not expect that when flipping a coin a finite number of times.

QuTiP is doing a statevector simulation and computing expectation values. Expectation values are what you would get having performed the experiment infinity many times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment