# How can i run a quantum circuit with parameterized gates many times on quantum experience?

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.

• "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? – luciano Oct 3 '20 at 14:18
• 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 – Moein sarvaghad Oct 3 '20 at 14:54
• sure. here it is quantumcomputing.stackexchange.com/a/14005/1859 hopefully is not too much detail. – luciano Oct 4 '20 at 3:10

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

from qiskit import IBMQ
IBMQ.save_account('MY_API_TOKEN')


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()
print(provider.backends())


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)
circuit.measure_all()
print(circuit)

        ┌───────┐ ░ ┌─┐
q_0: ┤ RY(x) ├─░─┤M├
└───────┘ ░ └╥┘
meas: 1/═════════════╩═
0


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:

job.wait_for_final_state()

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}

• Thanks a lot for your helping. – Moein sarvaghad Oct 4 '20 at 20:09
• Thank you again. i accepted. – Moein sarvaghad Oct 6 '20 at 8:06
• 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. – Moein sarvaghad Oct 6 '20 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.ry(variable[i],0)
qc.cx([0],[1])
qc.measure([0,1], [0,1])
print( qc )

• 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. :) – Cryoris Oct 3 '20 at 17:10
• 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?. – Moein sarvaghad Oct 3 '20 at 17:11