# Qiskit: how can you use a different state for each shot in a quantum circuit? [duplicate]

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
$$$$

• @luciano this is not a duplicate: tge other question asks about varying the circuit while this one asks about varying the state. – user1271772 Feb 24 at 14:57

The initial state in a quantum computer is fixed. It is usually taken to the the state $$|0\rangle^{\otimes n}$$. To vary a single qubit state during the experiment, you must therefore vary the circuit during the experiment, and you can't vary the circuit between each shot. However, if you wish to do what you proposed, you can create a list of circuits and each of them has vary quantum state $$|\psi\rangle$$, then append them together to a list and submit it as a single job.

For example:

from qiskit.quantum_info import random_state
from qiskit.aqua.components.initial_states import Custom
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.tools.monitor import job_monitor

num_qubits = 1
random_initial_state = random_state(2**num_qubits)  #create a random state psi
circuits = []
for i in range(100):
circuit = QuantumCircuit(num_qubits,num_qubits)
circuit.initialize(random_initial_state, 0)
for j in range(num_qubits):
circuit.measure([j],[j])
circuits.append(circuit)

qjob = execute(circuits,shots= 1,backend= Aer.get_backend('qasm_simulator'))
job_monitor(qjob)
result = qjob.result()
`

I believe that on the hardware, you can submit up to 900 circuits per job. So you can have up to 900 different states.