mirror of
https://github.com/quantumjim/Quantum-Computation-course-Basel.git
synced 2026-10-02 08:18:22 +02:00
43 KiB
43 KiB
In [29]:
from qiskit import *
from qiskit.quantum_info import Operator
from qiskit_aer import AerSimulator
from qiskit.visualization import plot_histogram
import numpy as np
from IPython.display import ImageIn [5]:
Image(url="grover_list.png", width=800, height=400)Out [5]:

In [7]:
Image(url="grover_circuit_high_level.png", width=1000, height=400)Out [7]:

In [8]:
Image(url="grover_step1.jpg", width=1000, height=400)Out [8]:

In [9]:
Image(url="grover_step2.jpg", width=1000, height=400)Out [9]:

In [12]:
Image(url="grover_step3.jpg", width=1000, height=400)Out [12]:

In [32]:
def oracle(n, marked_items):
# Create an n-qubit quantum circuit
qc = QuantumCircuit(n, name='Oracle')
### YOUR CODE GOES HERE - START
### YOUR CODE GOES HERE - END
# This converts your matrix into an operator and appends it to the circuit. Do not alter this line.
qc.unitary(Operator(oracle), range(n))
return qcIn [33]:
def diffuser(n, name='Diffuser'):
# Create an n-qubit quantum circuit
qc = QuantumCircuit(n, name='Diffuser')
### YOUR CODE GOES HERE - START
### YOUR CODE GOES HERE - END
return qcIn [34]:
def optimal_r(n, marked_items):
k = len(marked_items)
### YOUR CODE GOES HERE - START
### YOUR CODE GOES HERE - END
return rIn [52]:
def full_algorithm(n, marked_items):
qc = QuantumCircuit(n, n)
r = optimal_r(n, marked_items)
print('The optimal number of repetitions is r =', r)
# Step 1: State preparation
qc.h(range(n))
# Steps 2 and 3, repeated r times:
for _ in range(r):
qc.append(oracle(n, marked_items), range(n))
qc.append(diffuser(n), range(n))
# Measure all qubits
qc.measure(range(n), range(n))
return qc
grover = full_algorithm(6, [10, 32])
grover.draw()Out [52]:
The optimal number of repetitions is r = 4
┌───┐┌─────────┐┌───────────┐┌─────────┐┌───────────┐┌─────────┐»
q_0: ┤ H ├┤0 ├┤0 ├┤0 ├┤0 ├┤0 ├»
├───┤│ ││ ││ ││ ││ │»
q_1: ┤ H ├┤1 ├┤1 ├┤1 ├┤1 ├┤1 ├»
├───┤│ ││ ││ ││ ││ │»
q_2: ┤ H ├┤2 ├┤2 ├┤2 ├┤2 ├┤2 ├»
├───┤│ Oracle ││ Diffuser ││ Oracle ││ Diffuser ││ Oracle │»
q_3: ┤ H ├┤3 ├┤3 ├┤3 ├┤3 ├┤3 ├»
├───┤│ ││ ││ ││ ││ │»
q_4: ┤ H ├┤4 ├┤4 ├┤4 ├┤4 ├┤4 ├»
├───┤│ ││ ││ ││ ││ │»
q_5: ┤ H ├┤5 ├┤5 ├┤5 ├┤5 ├┤5 ├»
└───┘└─────────┘└───────────┘└─────────┘└───────────┘└─────────┘»
c: 6/════════════════════════════════════════════════════════════════»
»
« ┌───────────┐┌─────────┐┌───────────┐┌─┐
«q_0: ┤0 ├┤0 ├┤0 ├┤M├───────────────
« │ ││ ││ │└╥┘┌─┐
«q_1: ┤1 ├┤1 ├┤1 ├─╫─┤M├────────────
« │ ││ ││ │ ║ └╥┘┌─┐
«q_2: ┤2 ├┤2 ├┤2 ├─╫──╫─┤M├─────────
« │ Diffuser ││ Oracle ││ Diffuser │ ║ ║ └╥┘┌─┐
«q_3: ┤3 ├┤3 ├┤3 ├─╫──╫──╫─┤M├──────
« │ ││ ││ │ ║ ║ ║ └╥┘┌─┐
«q_4: ┤4 ├┤4 ├┤4 ├─╫──╫──╫──╫─┤M├───
« │ ││ ││ │ ║ ║ ║ ║ └╥┘┌─┐
«q_5: ┤5 ├┤5 ├┤5 ├─╫──╫──╫──╫──╫─┤M├
« └───────────┘└─────────┘└───────────┘ ║ ║ ║ ║ ║ └╥┘
«c: 6/══════════════════════════════════════╩══╩══╩══╩══╩══╩═
« 0 1 2 3 4 5 In [48]:
backend = AerSimulator()
circuit = transpile(grover, backend)
job = backend.run(circuit, shots=2000)
counts = job.result().get_counts()
plot_histogram(counts)Out [48]:
In [ ]: