qiskit/releasenotes/notes/1.1/classical-store-e64ee128621...

70 lines
3.1 KiB
YAML

---
features_circuits:
- |
A :class:`.QuantumCircuit` can now contain typed classical variables::
from qiskit.circuit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit.circuit.classical import expr, types
qr = QuantumRegister(2, "q")
cr = ClassicalRegister(2, "c")
qc = QuantumCircuit(qr, cr)
# Add two input variables to the circuit with different types.
a = qc.add_input("a", types.Bool())
mask = qc.add_input("mask", types.Uint(2))
# Test whether the input variable was true at runtime.
with qc.if_test(a) as else_:
qc.x(0)
with else_:
qc.h(0)
qc.cx(0, 1)
qc.measure(qr, cr)
# Add a typed variable manually, initialized to the same value as the classical register.
b = qc.add_var("b", expr.lift(cr))
qc.reset([0, 1])
qc.h(0)
qc.cx(0, 1)
qc.measure(qr, cr)
# Store some calculated value into the `b` variable.
qc.store(b, expr.bit_and(b, cr))
# Test whether we had equality, up to a mask.
with qc.if_test(expr.equal(expr.bit_and(b, mask), mask)):
qc.x(0)
These variables can be specified either as *inputs* to the circuit, or as scoped variables.
The circuit object does not yet have support for representing typed classical-variable *outputs*,
but this will be added later when hardware and the result interfaces are in more of a position
to support it. Circuits that represent a block of an inner scope may also capture variables
from outer scopes.
A variable is a :class:`.Var` node, which can now contain an arbitrary type, and represents a
unique memory location within its live range when added to a circuit. These can be constructed
in a circuit using :meth:`.QuantumCircuit.add_var` and :meth:`~.QuantumCircuit.add_input`, or
at a lower level using :meth:`.Var.new`.
Variables can be manually stored to, using the :class:`.Store` instruction and its corresponding
circuit method :meth:`.QuantumCircuit.store`. This includes writing to :class:`.Clbit` and
:class:`.ClassicalRegister` instances wrapped in :class:`.Var` nodes.
Variables can be used wherever classical expressions (see :mod:`qiskit.circuit.classical.expr`)
are valid. Currently this is the target expressions of control-flow operations, though we plan
to expand this to gate parameters in the future, as the type and expression system are expanded.
See :ref:`circuit-repr-real-time-classical` for more discussion of these variables, and the
associated data model.
These are supported throughout the transpiler, through QPY serialization (:mod:`qiskit.qpy`),
OpenQASM 3 export (:mod:`qiskit.qasm3`), and have initial support through the circuit visualizers
(see :meth:`.QuantumCircuit.draw`).
.. note::
The new classical variables and storage will take some time to become supported on hardware
and simulator backends. They are not supported in the primitives interfaces
(:mod:`qiskit.primitives`), but will likely inform those interfaces as they evolve.