I am trying to run a quantum circuit many times, each time using a different random state - a single qubit i.e. $|\psi\rangle= a|0\rangle + b|1\rangle$ with different $a$ and $b$. Once each qubit has gone through the circuit, I am saving the state vector of the final state, and converting this to spherical coordinates by calculating $\theta$ and $\phi$. I append each $\theta$ and $\phi$ value to a list and then plot them on a histogram (omitted below).
Whilst this works, for large num_states
it is very slow, so I am wondering if there is a way to use the 'shots' feature when executing the circuit, since you can write:
job = execute(qc, backend, shots=1000)
I tried playing around with this but found after the initial random state was generated, it repeated the circuit with the same state for the number of shots, rather than doing a different state each time. I'd like to use the shots feature as I believe this will be much quicker since OpenMP is built into Qiskit.
Any ideas on how I might be able to implement a different state each time using shots would be much appreciated:)
import numpy as np
from qiskit import(
QuantumCircuit,
execute,
Aer)
from math import pi
from qiskit.quantum_info import random_statevector
#----------------------------------------------------------------------#
backend = Aer.get_backend('statevector_simulator')
def simulate_states(num_states):
qc = QuantumCircuit(1)
theta_vals = []
phi_vals = []
for i in range(num_states):
random_state = random_statevector(2).data
qc.initialize(random_state, 0)
qc.z(0)
job = execute(qc, backend)
result = job.result()
out_state = result.get_statevector()
theta, phi, alpha_r, alpha_i, beta_r, beta_i = state_coords(out_state)
theta += pi/2
theta_vals.append(theta)
phi_vals.append(phi)
return theta_vals, phi_vals
def state_coords(statevector):
"""
determines the spherical coordinates of a state on the Bloch sphere i.e. calculates \theta and \phi.
"""
alpha = statevector[0]
alpha_r = alpha.real
alpha_i = alpha.imag
beta = statevector[1]
beta_r = beta.real
beta_i = beta.imag
theta = np.arcsin(2*((alpha_r*beta_r) - (alpha_i*beta_i)))
phi = 2*np.arccos(2*((alpha_r**2)+(beta_r**2))-1)
return theta, phi, alpha_r, alpha_i, beta_r, beta_i
```