1
$\begingroup$

I had a question about obtaining the probability of a certain state from the histogram, still using this example, suppose there's a variable in my quantum circuit, it's values are stored in an 'np.linspace' array. This is the result obtained from one of the elements in that array: enter image description here If I'm only interested in the probability of the '0010' state (0.831 here), is there a faster way I can get a list (or array) of the probability of '0010' using each element in my original linspace? I tried to incorporate a few commands (like job_sim and result_sim) into a function, but that doesn't work.

ps: The structure of my function looks something like this:

from qiskit import IBMQ
provider = IBMQ.load_account()
sim = provider.backends.ibmq_qasm_simulator
def qc(Variable):
    qcz = QuantumCircuit(qrz,crz)
    ......
    job_sim = execute(qcz, sim)
    result_sim = job_sim.result()
    counts = result_sim.get_counts()
    probs = counts['0010'] / sum(counts.values())
    return probs

After that I want to call the function using each of the elements in my linspace.

update:Sorry maybe I was a bit unclear in my original question, if my simulation result is determined by some variable X, for each value of X, I might obtain a different probability of the state '0010'. If there're N values of X, all of which are stored in an array and I want to run my circuit N times, is there a faster way I can do that instead of manually changing the variable and record the result? Thanks for the help:)

$\endgroup$
7
  • $\begingroup$ sorry I missed read your question earlier :) $\endgroup$
    – KAJ226
    Commented Dec 7, 2020 at 6:47
  • $\begingroup$ @KAJ226 No problem, thanks:) $\endgroup$
    – ZR-
    Commented Dec 7, 2020 at 6:52
  • 1
    $\begingroup$ You might find it useful to use a parametrized circuit as documented here: qiskit.org/documentation/stubs/… $\endgroup$
    – agiri
    Commented Dec 7, 2020 at 6:58
  • 1
    $\begingroup$ @Zhengrong so let's say $X$ has 10 variables, which resulted in 10 different circuits, are you then asking for a way to submit this 10 different circuits on a single job in Qiskit? instead of submit 10 different jobs with each job contains a single circuit. $\endgroup$
    – KAJ226
    Commented Dec 7, 2020 at 7:01
  • $\begingroup$ @KAJ226 Yes, this is exactly what I'm wondering:) $\endgroup$
    – ZR-
    Commented Dec 7, 2020 at 7:10

1 Answer 1

1
$\begingroup$

You can do 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_valencia')

def qc(Variable):
    var_form = QuantumCircuit(2,2)
    var_form.u1(Variable,0)
    var_form.cx(0,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, name='MyExperiment')
results = MyExperiments.results()

Note that qc(Variable) is spitting out circuit output of the form:

enter image description here

so the Ry gate is taking in the variable. You can modify the circuit to your choice.

Now when I run this, and look in the IBMQX results tabs I see the following:

enter image description here

Hope this helps.

$\endgroup$
1
  • $\begingroup$ Thanks, that's really helpful:) If I'm only interested in the probability of one of the states (say '11'), where should I put 'probs = counts['11'] / sum(counts.values())' so that I can get all the results in a list or array? $\endgroup$
    – ZR-
    Commented Dec 7, 2020 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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