1
$\begingroup$

I want to construct an ansatz circuit in Qiskit, so I need some parameters to act on the gates (e.g. RX(a), RY(b)). In the Qiskit tutorials I find a way to implement a parameter:

import numpy as np

theta_range = np.linspace(0, 2 * np.pi, 128)
circuits = [qc.bind_parameters({theta: theta_val})
            for theta_val in theta_range]
circuits[-1].draw()

There is only one parameter, theta. I want more parameters in the some range. How can I achieve that?

$\endgroup$
  • $\begingroup$ Thanks for asking a question! Could you add the definition of the qc variable? $\endgroup$ – Cryoris Sep 2 at 8:57
1
$\begingroup$

You can create an arbitrary number of parameters in your circuit by using the qiskit.circuit.Parameter class. Here's a brief example

from qiskit.circuit import Parameter, QuantumCircuit

# define your parameters
a, b, c = Parameter('a'), Parameter('b'), Parameter('c')

circuit = QuantumCircuit(2)
circuit.rx(a, 0)  # RX(a) on qubit 0
circuit.ry(b, 0)  # RY(b) on qubit 1
circuit.crz(c, 0, 1)  # CRZ(c) controlled on qubit 0, acting on qubit 1

# bind the values
bound_circuit = circuit.bind_parameters({a: 0, b: 1, c: 2})

# or if you have a list of values
a_vals = [0, 1, 2, 3]
b_vals = [1, 2, 3, 4]
c_vals = [2, 3, 4, 5]
bound_circuits = [circuit.bind_parameters(
    {a: a_val, b: b_val, c: c_val}) 
     for (a_val, b_val, c_val) in zip(a_vals, b_vals, c_vals)]

For convenience there's also the ParameterVector class to construct multiple parameters at once. The above example can also look like

from qiskit.circuit import ParameterVector, QuantumCircuit

# define your parameters
p = ParameterVector('p', 3)  

circuit = QuantumCircuit(2)
circuit.rx(p[0], 0)  # RX(p[0]) on qubit 0
circuit.ry(p[1], 0)  # RY(p[1]) on qubit 1
circuit.crz(p[2], 0, 1)  # CRZ(p[2]) controlled on qubit 0, acting on qubit 1

# bind the values
bound_circuit = circuit.bind_parameters({p: [0, 1, 2]})

# or for a list of values
values = [ [0, 1, 2], [1, 2, 3], [2, 3, 4] ]
bounds_circuits = [circuit.bind_parameters({p: val} for val in values})
| improve this answer | |
$\endgroup$
  • $\begingroup$ oh! thank you. i $\endgroup$ – wu Peter Sep 2 at 9:05
  • $\begingroup$ Thank you. but there's something wrong with your code. $\endgroup$ – wu Peter Sep 2 at 10:11
  • $\begingroup$ bound_circuits = [circuit.bind_parameters( {a: a_val, b: b_val, c: c_val for (a_val, b_val, c_val) in zip(a_vals, b_vals, c_vals)} )] There seems to be something wrong with this code. The error as follows: File "<ipython-input-4-78b6b27b9d79>", line 14 {a: a_val, b: b_val, c: c_val for (SyntaxError: invalid syntax) $\endgroup$ – wu Peter Sep 2 at 10:12
  • 1
    $\begingroup$ I also added an example for multiple values and the parameter vector $\endgroup$ – Cryoris Sep 2 at 14:02
  • 1
    $\begingroup$ Ah, just a little bit! You can join the Slack community, there's many people that know Qiskit very well: ibm.co/joinqiskitslack. $\endgroup$ – Cryoris Sep 3 at 8:38

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.