Skip to content

Instantly share code, notes, and snippets.

@frankzickert
Last active June 23, 2022 13:10
Show Gist options
  • Save frankzickert/36dbd444f6263eee2c30466a920d0900 to your computer and use it in GitHub Desktop.
Save frankzickert/36dbd444f6263eee2c30466a920d0900 to your computer and use it in GitHub Desktop.
from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister, Aer, execute
from qiskit.visualization import plot_histogram
DIGITS = 3
def oracle(secret, as_gate=True):
o_qc = QuantumCircuit(DIGITS+1)
for i in range(DIGITS):
if secret[::-1][i] == '1':
o_qc.cx(i,DIGITS)
else:
o_qc.i(i)
return o_qc.to_gate(label="oracle") if as_gate else o_qc
cr = ClassicalRegister(DIGITS, "cr")
qr = QuantumRegister(DIGITS, "digits")
aux = QuantumRegister(1, "aux")
qc = QuantumCircuit(qr, aux, cr)
qc.h(qr)
qc.x(aux)
qc.h(aux)
qc.append(oracle('111'), [*qr, *aux])
qc.h(qr)
qc.measure(qr, cr)
results = execute(qc,Aer.get_backend('qasm_simulator'), shots=1000).result().get_counts()
plot_histogram(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment