Last active
June 23, 2022 13:10
-
-
Save frankzickert/36dbd444f6263eee2c30466a920d0900 to your computer and use it in GitHub Desktop.
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
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