mirror of
https://github.com/quantumjim/Quantum-Computation-course-Basel.git
synced 2026-09-30 12:48:22 +02:00
56 KiB
56 KiB
In [4]:
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
from qiskit.visualization import plot_histogram
import numpy as np
from scipy.linalg import expmIn [19]:
qc = QuantumCircuit(2,2)
qc.h(0)
qc.cx(0,1)
qc.measure([0,1],[0,1])
qc.draw()Out [19]:
┌───┐ ┌─┐
q_0: ┤ H ├──■──┤M├───
└───┘┌─┴─┐└╥┘┌─┐
q_1: ─────┤ X ├─╫─┤M├
└───┘ ║ └╥┘
c: 2/═══════════╩══╩═
0 1 In [20]:
backend = AerSimulator()
job = backend.run(qc, shots=1024)In [21]:
output = job.result()
counts = output.get_counts()
plot_histogram(counts)Out [21]:
In [29]:
#Use this as a skeleton
qc2 = QuantumCircuit(2,2)
#Prepare |Psi^+> (do not alter)
qc2.h(0)
qc2.cx(0,1)
## YOUR CODE GOES HERE ###Out [29]:
<qiskit.circuit.instructionset.InstructionSet at 0x122d7bb20>
In [ ]:
job = backend.run(qc2, shots=1024)
counts = job.result().get_counts()
plot_histogram(counts)In [5]:
X = np.array([[0,1],[1,0]])
Y = np.array([[0,-1.j],[1.j,0]])
Z = np.array([[1,0],[0,-1]])
H = 1/np.sqrt(2)*np.array([[1,1],[1,-1]])In [6]:
def rotation(n, theta, varphi):
"ARGS: unit vector n=[nx,ny,nz], theta, varphi"
n_dot_sigma=(n[0]*X + n[1]*Y + n[2]*Z)
U = expm(1.j*theta*n_dot_sigma)
rot = np.exp(1.j*varphi)*U
return rot
In [8]:
your_Hadamard = rotation(### YOUR ANSWER GOES HERE###)
np.allclose(H, your_Hadamard)Out [8]:
True
In [105]:
error = {}
for n in range(1,11):
qc = QuantumCircuit(1,1)
# Implement the Trotterized Hadamard
### YOUR ANSWER GOES HERE
theta = np.pi
###
for j in range(n):
qc.rx(theta/n,0)
qc.rz(theta/n,0)
# We need to measure how good the above approximation is. Here's a simple way to do this.
# Step 1: Use a real Hadamard to cancel the above approximation.
# For a perfect approximation, the qubit will return to its initial state (0) since H squares to the identity.
qc.h(0)
# Step 2: Run the circuit, and see how many times we get the outcome 1. The fraction of 1s is a measure of the error.
qc.measure(0,0)
shots = 20000
job = backend.run(qc, shots=shots)
try:
error[n] = (job.result().get_counts()['1']/shots)
except:
pass
plot_histogram(error)Out [105]:
In [114]:
n_vals = []
error_vals = []
for key, val in error.items():
n_vals.append(key)
error_vals.append(val)In [ ]: