mirror of
https://github.com/quantumjim/Quantum-Computation-course-Basel.git
synced 2026-09-30 04:38:22 +02:00
40 KiB
40 KiB
In [1]:
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 [2]:
Image(url="grover_list.png", width=800, height=400)Out [2]:

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

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

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

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

In [7]:
def oracle(n, marked_items):
# Create an n-qubit quantum circuit
qc = QuantumCircuit(n, name='Oracle')
### YOUR CODE GOES HERE - START
oracle = np.identity(2**n)
for index in marked_items:
oracle[index, index] = -1
### 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 [11]:
def diffuser(n, name='Diffuser'):
# Create an n-qubit quantum circuit
qc = QuantumCircuit(n, name='Diffuser')
### YOUR CODE GOES HERE - START
qc.h(range(n))
qc.append(oracle(n, [0]), range(n))
qc.h(range(n))
### YOUR CODE GOES HERE - END
return qcIn [12]:
def optimal_r(n, marked_items):
k = len(marked_items)
### YOUR CODE GOES HERE - START
r = int(np.floor(np.pi/4*np.sqrt(2**n/k)))
### YOUR CODE GOES HERE - END
return rIn [13]:
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 [13]:
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 [14]:
backend = AerSimulator()
circuit = transpile(grover, backend)
job = backend.run(circuit, shots=2000)
counts = job.result().get_counts()
plot_histogram(counts)Out [14]:
In [ ]: