question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Name-sorted parameters in `qiskit.opflow`

See original GitHub issue

What is the expected enhancement?

Binding parameters to a parameterized Opflow objects requires passing a dictionary with {parameter_instance: value} pairs. More often than not, this is used in the following setting

my_circuit = # ...
expression = # some opflow expression using the circuit

param_dict = dict(zip(my_circuit.parameters, my_values))
bound_expression = expression.bind_parameters(param_dict)

which is a bit cumbersome as we either have to carry along the parameters we’re interested in or have to write an extra line defining the param_dict.

Instead if would be nice to allow binding the parameters using only an array. Since we introduced a global parameter order in #5759, which says that parameters are sorted by name, that would be easy to implement. Using the same ordering convention as for the circuits, we could simply write

my_circuit = # ...
expression = # some opflow expression using the circuit

bound_expression = expression.bind_parameters(values)

Since Opflow objects already implement the parameter attribute, the steps to implement this could for instance be

  1. ensure the parameter attribute returns a ParameterView object with parameters in the right order
  2. if an array is passed into bind_parameters, construct the parameter dictionary for the user using the parameter order from parameters

I’m labelling this as “good first issue”, but some previous experience with qiskit.opflow would certainly be helpful!

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
Cryoriscommented, May 31, 2022

Hi @pradkrish, for the QuantumCircuit we’re already able to pass my_values. But here’s a slightly modified example where we are currently not able to pass an array but would like to

from qiskit.circuit import Parameter, QuantumCircuit
from qiskit.opflow import StateFn, Z

phase = Parameter("phase")
x = Parameter("x")
y = Parameter("y")
z = Parameter("z")

qc = QuantumCircuit(1, global_phase=phase)
qc.rx(x, 0)
qc.rz(z, 0)
qc.ry(y, 0)

a = Parameter("a")

my_values = [-1, 0.1, 10, 20, 30]  # a, phase, x, y ,z

expectation = StateFn(a * Z, is_measurement=True) @ StateFn(qc)
expectation.assign_parameters(my_values)  # not possible right now!
1reaction
pradkrishcommented, May 23, 2022

Hello, is this issue still open? I am new to this project, happy to work on this with some guidance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Operator Flow - Qiskit
In the last section we construct a state, evolve it with a Hamiltonian, and compute expectation values of an observable. Pauli operators, sums,...
Read more >
Release Notes - Qiskit
Qiskit Metapackage Version qiskit‑terra qiskit‑aer qiskit‑ibmq‑provider Release Date 0.39.3 0.22.3 0.11.1 0.19.2 2022‑11‑25 0.39.2 0.22.2 0.11.1 0.19.2 2022‑11‑03 0.39.1 0.22.1 0.11.1 0.19.2 2022‑11‑02
Read more >
Release Notes — Qiskit 0.36.0 documentation
Qiskit Metapackage Version qiskit‑terra qiskit‑aer qiskit‑ignis qiskit‑ibmq‑provider Release... 0.36.0 0.20.0 0.10.4 0.7.0 0.19.0 2022‑04... 0.35.0 0.20.0 0.10.3 0.7.0 0.18.3 2022‑03... 0.34.2 0.19.2 0.10.3 0.7.0 0.18.3 2022‑02......
Read more >
Operators ( qiskit.opflow )
3. An n-qubit Measurement is a functional taking n-qubit State functions to complex values. For example, a Pauli Z Measurement can be defined...
Read more >
CircuitOp - Qiskit
Bases: qiskit.opflow.primitive_ops.primitive_op. ... Binds scalar values to any Terra Parameters in the coefficients or primitives of the Operator, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found