I want to run a quantum circuit many times on real hardware so that this circuit has some of the parameters that I must change them each time (some of the angles of gates must change each time). how can I run a circuit with these features on quantum experience?. can I use the "for" statement and the hardware can compile it? generally, How can I write the code for doing this work?. I would be very grateful if anyone helps me.

  • $\begingroup$ "real hardware" and "quantum experience" are not synonyms. This can be done very straightforward with Qiskit, on real quantum hardware. Are you familiar with Qiskit? $\endgroup$ – luciano Oct 3 at 14:18
  • $\begingroup$ Thanks for your comments. I know about Qiskit a little, is it possible, you send me a template code with a simple circuit for running on real hardware with the mentioned features?. I would be very grateful. thanks in advance $\endgroup$ – Moein sarvaghad Oct 3 at 14:54
  • $\begingroup$ sure. here it is quantumcomputing.stackexchange.com/a/14005/1859 hopefully is not too much detail. $\endgroup$ – luciano Oct 4 at 3:10

You first need to pip install qiskit and get your API Token from https://quantum-computing.ibm.com/account

enter image description here

Then, save your token in your configuration:

from qiskit import IBMQ

In this way, your Qiskit installation now is connected with your IBM Quantum Experience account (aka, a provider). You can list all your devices like this:

from qiskit import IBMQ

provider = IBMQ.get_provider()

Choose a device in which your circuit fits and set it as backend. For example, ibmq_16_melbourne:

backend = provider.get_backend('ibmq_16_melbourne')

Then, create your circuit with qiskit.circuit.Parameter for the parametric part. For example, take this dummy scenario:

from qiskit import *
from qiskit.circuit import Parameter 
parameter = Parameter('x')

circuit = QuantumCircuit(1)
circuit.ry(parameter, 0)
        ┌───────┐ ░ ┌─┐
   q_0: ┤ RY(x) ├─░─┤M├
        └───────┘ ░ └╥┘
meas: 1/═════════════╩═

You can create a set of circuits using different parameters with bind_parameters:

from numpy import pi

circuits = []
for param in [pi/2, pi/3, pi/5]:
    circuits.append(circuit.bind_parameters({parameter: param}))

Finally, you are ready to send the job:

job = execute(circuits, backend=backend)

For checking the results, you have to wait until the job is done:


for idx, param in enumerate([pi/2, pi/3, pi/5]):
    print(param, job.result().get_counts(idx))
1.5707963267948966 {'1': 504, '0': 520}
1.0471975511965976 {'0': 770, '1': 254}
0.6283185307179586 {'0': 918, '1': 106}
| improve this answer | |
  • $\begingroup$ Thanks a lot for your helping. $\endgroup$ – Moein sarvaghad Oct 4 at 20:09
  • $\begingroup$ please, accept the answer if it answered the question. $\endgroup$ – luciano Oct 4 at 23:55
  • $\begingroup$ Thank you again. i accepted. $\endgroup$ – Moein sarvaghad Oct 6 at 8:06
  • $\begingroup$ Can I run circuits on real hardware as offline?. I mean that a job with 100 circuits may take a long time and I want to shot down my laptop in running time. in the meantime, Can the real hardware run all of the circuits in the job, and in another time I can obtain my results?. or it is necessary that my laptop turn on till I can receive the last result?. if I need any code for this issue, please explain it. $\endgroup$ – Moein sarvaghad Oct 6 at 8:55

A simple example:

from qiskit import QuantumCircuit
variable = [0.2, 0.5, 1.2, -0.4, -1.5, 1.7 ]  
for i in range(len(variable)):
    qc = QuantumCircuit(2,2)
    qc.measure([0,1], [0,1])
    print( qc ) 
| improve this answer | |
  • $\begingroup$ This doesn’t yet run the circuit on the real hardware, you could consider adding that part. Also you can use the qiskit.circuit.Parameter object to parameterize the circuit and bind it to different values instead of reconstructing it each time. :) $\endgroup$ – Cryoris Oct 3 at 17:10
  • $\begingroup$ thanks a lot. How can I run this code on real hardware?. Must I add other statements to it such as properties of the processor?. $\endgroup$ – Moein sarvaghad Oct 3 at 17:11

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.