Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name-sorted parameters in qiskit.opflow #7802

Closed
Cryoris opened this issue Mar 22, 2022 · 12 comments
Closed

Name-sorted parameters in qiskit.opflow #7802

Cryoris opened this issue Mar 22, 2022 · 12 comments
Labels
good first issue Good for newcomers mod: opflow Related to the Opflow module type: enhancement It's working, but needs polishing

Comments

@Cryoris
Copy link
Contributor

Cryoris commented Mar 22, 2022

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!

@Cryoris Cryoris added type: enhancement It's working, but needs polishing good first issue Good for newcomers mod: opflow Related to the Opflow module labels Mar 22, 2022
@Cryoris Cryoris changed the title Bind parameters in Opflow objects by array Name-sorted parameters in qiskit.opflow Mar 22, 2022
@pradkrish
Copy link
Contributor

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

@jakelishman
Copy link
Member

Sure, thanks! I'll assign you now, so others know you're working on it. Feel free to ask questions here - @Cryoris knows rather more about opflow than I do, so he's the best person to ask.

@Cryoris
Copy link
Contributor Author

Cryoris commented May 25, 2022

Nice to see you're interested! You can check out how the parameters are sorted in the quantum circuit
https://github.com/Qiskit/qiskit-terra/blob/d2e3340adb79719f9154b665e8f6d8dc26b3e0aa/qiskit/circuit/quantumcircuit.py#L2373

and how they are assigned if not a dictionary but only a list of values is passed
https://github.com/Qiskit/qiskit-terra/blob/d2e3340adb79719f9154b665e8f6d8dc26b3e0aa/qiskit/circuit/quantumcircuit.py#L2495

Let us know if you have any questions!

@pradkrish
Copy link
Contributor

I made an attempt to construct a circuit with parameters like this

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)

my_values = [0.1,10,20,30]
param_dict = dict(zip(qc.parameters, my_values))

qc.bind_parameters(param_dict) <---

Is this a good working example for this issue where instead of passing param_dict, we would like to be able to pass my_values to bind_parameters?

@Cryoris
Copy link
Contributor Author

Cryoris commented 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!

@Cryoris
Copy link
Contributor Author

Cryoris commented Jul 6, 2022

Hi @pradkrish any news on this? 🙂

@nssensalo
Copy link

@Cryoris, Well I'd love to give it a try. I'm dying to start my first open source contribution. May I be assigned?

If not using array, can the parameters of "assign_parameters" be in a dictionary or boolean format?
Am I going down the right rabbit hole?

This snippiet is from the qiskit docs:

image

@Cryoris
Copy link
Contributor Author

Cryoris commented Jul 25, 2022

Hi @nssensalo, of course you can work on this, I'll assign you 🙂

For opflow we don't allow inplace right now, so the input to assign_parameters would only be one argument that is either a dictionary of parameter values (like right now) or an array of values (this would be new!).

@Cryoris Cryoris assigned nssensalo and unassigned pradkrish Jul 25, 2022
@AbhirMehrotra
Copy link

@Cryoris Hello, my friend and I wanted to work on this issue if it is still open. This is our first project but we are interested and are happy to work on it.

@GaneshH0571
Copy link

@Cryoris Hello, my friend and I would like to work on this issue, This is our first project but we are interested and happy to work on it

@javabster
Copy link
Contributor

hi @GaneshH0571 @AbhirMehrotra this issue has already been assigned to @nssensalo, there are many other open good first issues that haven't been assigned yet if you would like to take a look at those!

for @nssensalo if you're new to open source and contributing to qiskit I recommend taking a look at these resources:

Or if you are no longer interested in working on this also let us know so we can un-assign you and let someone else be assigned to it 😄

@javabster
Copy link
Contributor

update: issue has been unassigned and open for other contributors to jump in 😄

@javabster javabster moved this from Assigned to Tagged but unassigned in Contributor Monitoring Nov 17, 2022
@javabster javabster closed this as not planned Won't fix, can't repro, duplicate, stale Feb 2, 2023
@github-project-automation github-project-automation bot moved this from Tagged but unassigned to Done in Contributor Monitoring Feb 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers mod: opflow Related to the Opflow module type: enhancement It's working, but needs polishing
Projects
Status: Done
Development

No branches or pull requests

7 participants