0

I would like to make a code that is executed iteratively.

For example: in a one-qubit system, I would like to make a measurement 100 times in each angle theta = [ 0, ..., 2π] with the circuit as follows.

circuit

Can we make an iterative code as follows? (This is completely wrong !)

theta = 0
for theta in np.arange(0, np.pi, np.pi/100):
    qreg q[1]
    creg c[1]
    ry(theta) q[0]
    measure q[0] -> c[0]

In addition, can we make a function whose argument is theta as follows? (This is completely wrong !)

function q_citcuit(theta)
    qreg q[1]
    creg c[1]
    ry(theta) q[0]
    measure q[0] -> c[0]
    return result

id_zo
  • 1
  • 2

2 Answers2

0

In addition to specifying the gates for each iteration, you'd need to define a circuit and registers, running and measuring each time. Here's a Jupyter notebook that demonstrates these concepts: https://nbviewer.jupyter.org/github/Qiskit/qiskit-tutorials/blob/master/qiskit/basics/1_getting_started_with_qiskit.ipynb

James Weaver
  • 113
  • 4
0

Once you go through the tutorial James sent you and get the basics down, I think the following information will be helpful for your specific experiment you want to run. You can execute a list of circuits in one execution call, and you can create these circuits iteratively.

theta_list = [...]  # Whatever angles you want to put in here
circuit_list = []  # This will be what is executed

# After instantiating the QuantumRegisters and ClassicalRegisters as qreg and creg
for index in range(len(theta_list)):
   qc = QuantumCircuit(qreg, creg)
   qc.ry(theta_list[index], qreg[0])
   qc.measure(qreg[0], creg[0])
   circuit_list.append(qc)

# After choosing a backend
job = execute(circuit_list, backend, shots=100)

This should work for you, but, again, I would first look over the tutorial in James' answer to make sure you have the core concepts down and can fully understand what is going on in this code.