0

I am trying to run a quantum circuit many times, each time using a different random state - a single qubit i.e. |\psi>= a|0> + b|1> 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
e.deacon
  • 1
  • 1
  • 2
    Maybe ask this on the [Quantum Computing Stack Exchange.](https://quantumcomputing.stackexchange.com/) –  Feb 24 '21 at 09:43
  • what do you mean by counts? – luciano Feb 24 '21 at 10:02
  • @luciano I think I mean 'shots' actually. in the execute(qc, backend) part you can list the number of shots you want to circuit to be run for e.g. execute(qc, backend, shots=1000). if you look here you can see an example https://qiskit.org/documentation/tutorials/circuits/2_plotting_data_in_qiskit.html – e.deacon Feb 24 '21 at 10:12
  • This question might help: https://quantumcomputing.stackexchange.com/questions/8923/vary-parameter-between-shots – luciano Feb 24 '21 at 10:46

0 Answers0