# How to add several parameters in qiskit circuit?

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?

• Thanks for asking a question! Could you add the definition of the qc variable? Sep 2 '20 at 8:57

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

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

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})

• oh! thank you. i Sep 2 '20 at 9:05
• Thank you. but there's something wrong with your code. Sep 2 '20 at 10:11
• 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) Sep 2 '20 at 10:12
• I also added an example for multiple values and the parameter vector Sep 2 '20 at 14:02
• Ah, just a little bit! You can join the Slack community, there's many people that know Qiskit very well: ibm.co/joinqiskitslack. Sep 3 '20 at 8:38