7

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?

glS
  • 27,510
  • 7
  • 37
  • 125
wu Peter
  • 91
  • 1
  • 5

2 Answers2

7

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

Cryoris
  • 2,993
  • 8
  • 15
0

In addition to this thread. I've been facing some similar problems during Ansatz setup.

In my case, I used the RealAmplitudes ansatz from qiskit.circuit.library and needed to assign parameters manually for my specific use case. I haven't found any example in the documentation exemplifying what I needed, so I started playing a bit with the methods and found a solution.

When creating a ansatz like this:

from qiskit.circuit.library import RealAmplitudes

n_qubits = 6 n_blocks = 1

ansatz = RealAmplitudes(n_qubits, entanglement='full', reps=n_blocks, flatten=True)

Qiskit generates a circuit with $2*n$ rotations/parameters per block, being $n$ the number of qubits, so in total we have $p = (2 * n * b)-n$ parameters in total, where $b$ is the amount of blocks/reps and $p$ the total of parameters in the entire ansatz.

With that in mind, you can manually setup your parameters like:


import numpy as np

get_total_params = lambda n,b : (2nb)-n total_params = get_total_params(n_qubits, n_blocks)

params = np.random.uniform(0,np.pi,size=(total_params))

new_ansatz = ansatz.assign_parameters({ansatz.parameters[0].vector:params})


If you check the internal parameters with ansatz.parameters, you'll face a ParameterView object with $p$ ParameterVectorElements inside. They may seem different, however checking:

assert ansatz.parameters[0].vector != ansatz.parameters[1].vector, "They aren't different"

raises

AssertionError: They aren't different

Do to that fact, every Parameter Vector inside this object are virtually the same, so assigning parameters to the first vector is enough.


This example I gave was using qiskit v1.3.2, so different versions may raise different results.

Dpbm
  • 21
  • 3