Quantum Computing Asked on February 6, 2021
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.
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 )
Answered by KAJ226 on February 6, 2021
You first need to pip install qiskit
and get your API Token from https://quantum-computing.ibm.com/account
Then, save your token in your configuration:
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
IBMQ.load_account()
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}
Answered by luciano on February 6, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP