mirror of
https://github.com/quantumjim/Quantum-Computation-course-Basel.git
synced 2026-09-30 12:48:22 +02:00
16 KiB
16 KiB
In [1]:
from qiskit import *
%matplotlib inline
from qiskit.visualization import plot_histogram
from qiskit_aer import AerSimulator
# import image module
from IPython.display import Image
In [2]:
Image(url="classical.png", width=400, height=400)Out [2]:

In [ ]:
Image(url="quantum.png", width=400, height=400)In [16]:
secretnumber = input('Enter a binary number:')
l = len(secretnumber)Enter a binary number: 0101
In [17]:
qc = QuantumCircuit(l+1, l)
qc.h(range(l))
qc.x(l)
qc.h(l)
qc.barrier()
qc.draw()Out [17]:
┌───┐ ░
q_0: ┤ H ├──────░─
├───┤ ░
q_1: ┤ H ├──────░─
├───┤ ░
q_2: ┤ H ├──────░─
├───┤ ░
q_3: ┤ H ├──────░─
├───┤┌───┐ ░
q_4: ┤ X ├┤ H ├─░─
└───┘└───┘ ░
c: 4/═════════════
In [18]:
#Building the oracle
for i, digit in enumerate(reversed(secretnumber)):
if digit == '1':
qc.cx(i,l)
qc.draw()Out [18]:
┌───┐ ░
q_0: ┤ H ├──────░───■───────
├───┤ ░ │
q_1: ┤ H ├──────░───┼───────
├───┤ ░ │
q_2: ┤ H ├──────░───┼────■──
├───┤ ░ │ │
q_3: ┤ H ├──────░───┼────┼──
├───┤┌───┐ ░ ┌─┴─┐┌─┴─┐
q_4: ┤ X ├┤ H ├─░─┤ X ├┤ X ├
└───┘└───┘ ░ └───┘└───┘
c: 4/═══════════════════════
In [19]:
qc.h(range(l))
qc.measure(range(l), range(l))
qc.draw()Out [19]:
┌───┐ ░ ┌───┐ ┌─┐
q_0: ┤ H ├──────░───■──┤ H ├─────────────┤M├───
├───┤ ░ │ ├───┤ ┌─┐ └╥┘
q_1: ┤ H ├──────░───┼──┤ H ├─────┤M├──────╫────
├───┤ ░ │ └───┘ └╥┘┌───┐ ║ ┌─┐
q_2: ┤ H ├──────░───┼─────────■───╫─┤ H ├─╫─┤M├
├───┤ ░ │ ┌───┐ │ ║ └┬─┬┘ ║ └╥┘
q_3: ┤ H ├──────░───┼──┤ H ├──┼───╫──┤M├──╫──╫─
├───┤┌───┐ ░ ┌─┴─┐└───┘┌─┴─┐ ║ └╥┘ ║ ║
q_4: ┤ X ├┤ H ├─░─┤ X ├─────┤ X ├─╫───╫───╫──╫─
└───┘└───┘ ░ └───┘ └───┘ ║ ║ ║ ║
c: 4/═════════════════════════════╩═══╩═══╩══╩═
1 3 0 2 In [20]:
backend = AerSimulator()
result = backend.run(qc, shots=1).result()
counts = result.get_counts()
print(counts){'0101': 1}
In [ ]: