Skip to content

Instantly share code, notes, and snippets.

@silgon
Created September 18, 2023 15:46
Show Gist options
  • Save silgon/f198ec1086415b2fea769461d8404fbf to your computer and use it in GitHub Desktop.
Save silgon/f198ec1086415b2fea769461d8404fbf to your computer and use it in GitHub Desktop.
import qutip
import matplotlib.pyplot as plt
import numpy as np
b = qutip.Bloch()
# vec = [0,1,0]
th = np.linspace(0, np.pi, 20)
xz = np.sin(th)
yz = np.zeros(20)
zz = np.cos(th)
# b.add_vectors(vec)
b.clear()
# b.add_points([xz,yz,zz])
b.add_vectors(list(zip(xz,yz,zz)))
b.vector_color=["r"]
# b.add_vectors([xz,yz,zz])
b.make_sphere()
plt.show()
from math import pi
import matplotlib.pyplot as plt
from qiskit import *
from qiskit.tools.visualization import plot_bloch_multivector, plot_histogram
from qiskit.quantum_info import state_fidelity
from qiskit import BasicAer
import numpy as np
n = 7
qc = QuantumCircuit(n)
th_res = [] # theoretical results
for i in range(n):
θ = pi*i/(n-1)
qc.u(θ, 0, 0, i)
# compute theoretical results
tmp = {"0": np.cos(θ/2)**2, "1": np.sin(θ/2)**2}
th_res.append(tmp)
qc.barrier()
qc.measure_all()
qc.draw()
backend = BasicAer.get_backend('qasm_simulator')
# uncomment next 2 lines to use a quantum computer from IBM
# provider = IBMQ.get_provider("ibm-q")
# backend = provider.get_backend("ibm_perth")
shots = 1024
results = execute(qc, backend=backend, shots=shots).result()
answer = results.get_counts()
res = [{"0": 0, "1": 0} for _ in range(n)]
for i in range(n):
for k, v in answer.items():
res[i][k[i]] += v
res[i]["0"] /= shots
res[i]["1"] /= shots
df_th = pd.DataFrame(th_res)
df_res = pd.DataFrame(res[::-1]) # reversed order
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment