1
$\begingroup$

I'm running a couple of circuits at the same time using an array of variables. However, I met an issue regarding accessing the counts. Here's part of the code I have:

probsu = []
circuits = []

for i in range(len(Variable)):
    circuit =  QuantumCircuit(qrz,crz)
    circuit =  qc(Variable[i])
    circuits.append(circuit)
    prob = counts['1001'] / sum(counts.values()) **Issue here**
    probsu.append(prob)                          **Issue here**

circuits = transpile(circuits, backend=backend) 
job_manager = IBMQJobManager()
MyExperiments = job_manager. run(circuits, backend=backend, name='MyExperiment')
results = MyExperiments.results()
counts = results.get_counts(circuit)

I'm not able to get the counts since the line is referenced before the definition in the last line. However, there will be similar issues pop up if I remove the bottom lines before the for loop. How can I resolve this issue? Thanks:)

$\endgroup$
2
  • $\begingroup$ I think you asked me in a comment in one of your previous questions about something related to this... but I didn't reply to it. Sorry about that. $\endgroup$
    – KAJ226
    Dec 10 '20 at 17:47
  • $\begingroup$ @KAJ226 No problem, thanks for the help:) $\endgroup$
    – ZR-
    Dec 10 '20 at 18:39
1
$\begingroup$

You mean something like this?

%matplotlib inline
# Importing standard Qiskit libraries
from qiskit import QuantumCircuit, QuantumRegister,  IBMQ, ClassicalRegister
from qiskit.circuit import QuantumCircuit, ParameterVector
from qiskit.compiler import transpile, assemble
from qiskit.providers.ibmq.managed import IBMQJobManager
from qiskit.visualization import *
provider = IBMQ.load_account()
backend = provider.get_backend('ibmq_qasm_simulator')

def qc(Variable):
    var_form = QuantumCircuit(2,2)
    var_form.ry(Variable,0)
    var_form.cx(0,1) 
    var_form.h(1)
    var_form.measure([0,1],[0,1])
    return var_form

Variables = [1,2,3,4,5]
circuits = []
for i in range(len(Variables) ):
    circuit =  QuantumCircuit(2,2)
    circuit = qc(Variables[i])
    circuits.append( circuit   )


circuits = transpile(circuits, backend=backend) 
job_manager = IBMQJobManager()
MyExperiments = job_manager.run(circuits, backend=backend, shots =1000, name='MyExperiment')
results = MyExperiments.results()

probsu = []
for i in range(len(Variables) ):
    prob = results.get_counts(i)['11']/sum( results.get_counts(i).values() ) 
    probsu.append(prob) 
print('probability of getting |11> at each circuit:', probsu)

which generate the output:

probability of getting |11> at each circuit: [0.121, 0.33, 0.458, 0.417, 0.178]

the output above corresponds to the probabilities of reading out $|11\rangle$ on each of the 5 circuits... You can modify this circuit to your purpose... as this is only an example.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.