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

Parameters: Sorting & assigning #5614

Closed
Cryoris opened this issue Jan 12, 2021 · 1 comment
Closed

Parameters: Sorting & assigning #5614

Cryoris opened this issue Jan 12, 2021 · 1 comment
Labels
type: enhancement It's working, but needs polishing

Comments

@Cryoris
Copy link
Contributor

Cryoris commented Jan 12, 2021

This issues attempts to summarize possible enhancements for assigning parameters to circuits along with the main use-cases. Related to #5557 and #3910.

Status quo

To assign parameters to a circuit, one needs to pass

  • a dictionary of {parameter_instance: value}, or
  • a dictionary of {parameter_vector_instance: values_list}

The keys of the dictionary have to be the same instances as in the circuit.

Desired features

  1. Allow assigning a list of values to a circuit to bind all parameters anonymously, i.e. without specifying the parameters they belong to.
  2. Allow assigning values via name instead of instance. This is possible since parameter names are already required to be unique.

Use cases

I might forget some cases here, so feel free to add 🙂

From optimization algorithms:

  • Pretty much all classical optimization routines handle parameters as lists of values; including the numerical optimization routines used in algos like the VQE or QAOA. Since parameters are not sorted, running each optimization run might optimize the values in a different order, which leads to different results. The algorithms currently manually ensure that the parameters are sorted in a consistent manner -- though this might not be the order the user added them to the circuit, which has already led to some confusion in the past.
    If the parameters were (insertion) sorted by default and one could assign a plain vector, all this logic would fall away and circuits could naturally interact with optimizers. This works e.g. nicely in pennylane.

From gradients:

  • Constructing gradients or hessian currently works like this
    ansatz = # some ansatz with parameters [x, y, z, ...]
    expectation = # some expectation based on ansatz
    
    grad = Gradient().convert(expectation, params=[x, y, z, ...]) 
    hess = Hessian().convert(expectation, params=[x, y, z, ...])
    the parameters have to be carried along and passed to the gradients, since this specifies the order of the vector or matrix entries. It would be a lot more convenient to just call
    grad = Gradient().convert(expectation) 
    hess = Hessian().convert(expectation)
    but this would also require some sorting, which might not be what users expect. This can make comparison to other frameworks and validation difficult; where the usual order is by insertion.

Consistency:

  • Some library circuits already have an ordered_parameters attribute, which exists for (1) reproducibility, (2) backward compatibility with some old variational forms (and (3) convenience). If we had sorted parameters, this attribute would no longer be needed (also mentioned in Make it easier to assign parameters to vector of parameters #5557).

From #5557:

  • it would be more convenient to type
    x = Parameter('x')    
    circuit = QuantumCircuit(1)  
    circuit.rx(x, 0)
    circuit.assign_parameters({'x': 1})
    or even for a full parametervector, x = ParameterVector('x', 10) and {'x': [0, 1, 2, ...}. This can be particularly useful if (1) the circuit is passed around or (2) users are playing with circuits and interact a lot by plotting.
@Cryoris
Copy link
Contributor Author

Cryoris commented Mar 29, 2021

Resolved via #5759.

@Cryoris Cryoris closed this as completed Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement It's working, but needs polishing
Projects
None yet
Development

No branches or pull requests

1 participant