You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ensure QuantumCircuit.append validates captures in control-flow (#10974)
* Add definition of `Store` instruction
This does not yet add the implementation of `QuantumCircuit.store`,
which will come later as part of expanding the full API of
`QuantumCircuit` to be able to support these runtime variables.
The `is_lvalue` helper is added generally to the `classical.expr` module
because it's generally useful, while `types.cast_kind` is moved from
being a private method in `expr` to a public-API function so `Store` can
use it. These now come with associated unit tests.
* Add variable-handling methods to `QuantumCircuit`
This adds all the new `QuantumCircuit` methods discussed in the
variable-declaration RFC[^1], and threads the support for them through
the methods that are called in turn, such as `QuantumCircuit.append`.
It does yet not add support to methods such as `copy` or `compose`,
which will be done in a follow-up.
The APIs discussed in the RFC necessitated making `Var` nodes hashable.
This is done in this commit, as it is logically connected. These nodes
now have enforced immutability, which is technically a minor breaking
change, but in practice required for the properties of such expressions
to be tracked correctly through circuits.
A helper attribute `Var.standalone` is added to unify the handling of
whether a variable is an old-style existing-memory wrapper, or a new
"proper" variable with its own memory.
[^1]: Qiskit/RFCs#50
* Support manual variables `QuantumCircuit` copy methods
This commit adds support to the `QuantumCircuit` methods `copy` and
`copy_empty_like` for manual variables. This involves the non-trivial
extension to the original RFC[^1] that variables can now be
uninitialised; this is somewhat required for the logic of how the
`Store` instruction works and the existence of
`QuantumCircuit.copy_empty_like`; a variable could be initialised with
the result of a `measure` that no longer exists, therefore it must be
possible for variables to be uninitialised.
This was not originally intended to be possible in the design document,
but is somewhat required for logical consistency. A method
`add_uninitialized_var` is added, so that the behaviour of
`copy_empty_like` is not an awkward special case only possible through
that method, but instead a complete part of the data model that must be
reasoned about. The method however is deliberately a bit less
ergononmic to type and to use, because really users _should_ use
`add_var` in almost all circumstances.
[^1]: Qiskit/RFCs#50
* Ensure `QuantumCircuit.append` validates captures in control-flow
This adds an inner check to the control-flow operations that their
blocks do not contain input variables, and to `QuantumCircuit.append`
that any captures within blocks are validate (in the sense of the
variables existing in the outer circuit).
In order to avoid an `import` on every call to `QuantumCircuit.append`
(especially since we're already eating the cost of an extra
`isinstance` check), this reorganises the import structure of
`qiskit.circuit.controlflow` to sit strictly _before_
`qiskit.circuit.quantumcircuit` in the import tree. Since those are key
parts of the circuit data structure, that does make sense, although by
their nature the structures are of course recursive at runtime.
* Update documentation
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
* Catch simple error case in '_prepare_new_var'
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
* Add partial release note
---------
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
0 commit comments