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

[Stretch] Support Duration type in classical expressions. #13844

Merged
merged 96 commits into from
Mar 3, 2025
Merged
Changes from 9 commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
af4ba52
WIP
kevinhartman Feb 7, 2025
e423bf9
Add try_const to lift.
kevinhartman Feb 7, 2025
0a5917b
Try multiple singletons, new one for const.
kevinhartman Feb 7, 2025
a97434d
Revert "Try multiple singletons, new one for const."
kevinhartman Feb 7, 2025
1afc965
Remove Bool singleton test.
kevinhartman Feb 7, 2025
86655f1
Add const handling for stores, fix test bugs.
kevinhartman Feb 7, 2025
aaeae9b
Fix formatting.
kevinhartman Feb 7, 2025
a2a444b
Remove Duration and Stretch for now.
kevinhartman Feb 7, 2025
8ac2dc3
Cleanup, fix const bug in index.
kevinhartman Feb 7, 2025
9f8313c
Fix ordering issue for types with differing const-ness.
kevinhartman Feb 9, 2025
db9d9cb
Fix QPY serialization.
kevinhartman Feb 10, 2025
71b7e7a
Make expr.Lift default to non-const.
kevinhartman Feb 11, 2025
2091557
Revert to old test_expr_constructors.py.
kevinhartman Feb 11, 2025
7307be9
Make binary_logical lift independent again.
kevinhartman Feb 11, 2025
9b30284
Update tests, handle a few edge cases.
kevinhartman Feb 12, 2025
ce1faf1
Fix docstring.
kevinhartman Feb 12, 2025
4fee48f
Remove now redundant arg from tests.
kevinhartman Feb 12, 2025
88ab046
Add const testing for ordering.
kevinhartman Feb 12, 2025
c5a230f
Add const tests for shifts.
kevinhartman Feb 12, 2025
7c88d88
Add release note.
kevinhartman Feb 12, 2025
c58a7b8
Add const store tests.
kevinhartman Feb 12, 2025
d9e9a8c
Address lint, minor cleanup.
kevinhartman Feb 13, 2025
4a56150
Add Float type to classical expressions.
kevinhartman Feb 12, 2025
23b5961
Allow DANGEROUS conversion from Float to Bool.
kevinhartman Feb 12, 2025
8bf2e4f
Test Float ordering.
kevinhartman Feb 12, 2025
111eb32
Improve error messages for using Float with logical operators.
kevinhartman Feb 12, 2025
a839d51
Float tests for constructors.
kevinhartman Feb 12, 2025
a19b39a
Add release note.
kevinhartman Feb 12, 2025
7f02e56
Add Duration and Stretch classical types.
kevinhartman Feb 13, 2025
55af327
Add Duration type to qiskit.circuit.
kevinhartman Feb 13, 2025
86b7af9
Block Stretch from use in binary relations.
kevinhartman Feb 13, 2025
9b3c821
Add type ordering tests for Duration and Stretch.
kevinhartman Feb 13, 2025
69eab91
Test expr constructors for Duration and Stretch.
kevinhartman Feb 13, 2025
971cc26
Fix lint.
kevinhartman Feb 13, 2025
25508bf
Reject const vars in add_var and add_input.
kevinhartman Feb 17, 2025
2c8ce43
Merge branch 'main' of github.com:Qiskit/qiskit into const-expr
kevinhartman Feb 17, 2025
ccf9441
Implement QPY support for const-typed expressions.
kevinhartman Feb 18, 2025
c6eab02
Remove invalid test.
kevinhartman Feb 18, 2025
edd7806
Update QPY version 14 desc.
kevinhartman Feb 18, 2025
8afa92e
Fix lint.
kevinhartman Feb 18, 2025
4e0f2df
Add serialization testing.
kevinhartman Feb 18, 2025
15ba943
Merge branch 'main' of github.com:Qiskit/qiskit into const-expr
kevinhartman Feb 18, 2025
81c5833
Merge branch 'const-expr' into expr-float
kevinhartman Feb 18, 2025
50c31d3
Test pre-v14 QPY rejects const-typed exprs.
kevinhartman Feb 18, 2025
7e43322
Merge branch 'main' of github.com:Qiskit/qiskit into const-expr
kevinhartman Feb 18, 2025
2449ee6
Merge branch 'const-expr' into expr-float
kevinhartman Feb 18, 2025
e55e189
QASM export for floats.
kevinhartman Feb 18, 2025
1d51022
QPY support for floats.
kevinhartman Feb 18, 2025
eb8f150
Fix lint.
kevinhartman Feb 18, 2025
415f62e
Merge branch 'expr-float' into expr-timing
kevinhartman Feb 18, 2025
d38f3e9
Settle on Duration circuit core type.
kevinhartman Feb 19, 2025
fd66c1d
QPY serialization for durations and stretches.
kevinhartman Feb 19, 2025
e9b6d97
Add QPY testing.
kevinhartman Feb 19, 2025
52da3cc
QASM support for stretch and duration.
kevinhartman Feb 19, 2025
a3be688
Fix lint.
kevinhartman Feb 19, 2025
07771f1
Merge branch 'main' of github.com:Qiskit/qiskit into const-expr
kevinhartman Feb 20, 2025
9332ed3
Merge branch 'const-expr' into expr-float
kevinhartman Feb 20, 2025
17241c6
Merge branch 'expr-float' into expr-timing
kevinhartman Feb 20, 2025
ca91bfd
Don't use match since we still support Python 3.9.
kevinhartman Feb 20, 2025
ad87e78
Fix enum match.
kevinhartman Feb 20, 2025
d3bca5f
Merge branch 'main' of github.com:Qiskit/qiskit into const-expr
kevinhartman Feb 24, 2025
50deb93
Revert visitors.py.
kevinhartman Feb 24, 2025
f1dc1a1
Address review comments.
kevinhartman Feb 24, 2025
1f439a0
Merge branch 'main' of github.com:Qiskit/qiskit into const-expr
kevinhartman Feb 25, 2025
166d7b2
Improve type docs.
kevinhartman Feb 26, 2025
10b2c8d
Merge branch 'main' of github.com:Qiskit/qiskit into const-expr
kevinhartman Feb 26, 2025
9d6cf39
Revert QPY, since the old format can support constexprs.
kevinhartman Feb 26, 2025
8021e00
Move const-ness from Type to Expr.
kevinhartman Feb 26, 2025
a15141b
Revert QPY testing, no longer needed.
kevinhartman Feb 26, 2025
ca2785d
Add explicit validation of const expr.
kevinhartman Feb 26, 2025
e902009
Revert stuff I didn't need to touch.
kevinhartman Feb 26, 2025
16475c3
Update release note.
kevinhartman Feb 26, 2025
6b5930d
A few finishing touches.
kevinhartman Feb 27, 2025
be73180
Merge branch 'const-expr-expr' into expr-float-expr
kevinhartman Feb 27, 2025
25f1693
Fix-up after merge.
kevinhartman Feb 27, 2025
ecc343b
Merge branch 'expr-float-expr' into expr-timing-expr
kevinhartman Feb 27, 2025
b32e6f0
Fix-ups after merge.
kevinhartman Feb 27, 2025
864506d
Fix lint.
kevinhartman Feb 27, 2025
6bc728a
Fix comment and release note.
kevinhartman Feb 27, 2025
2d38178
Merge branch 'expr-float' into expr-timing
kevinhartman Feb 27, 2025
42258b8
Merge branch 'main' of github.com:Qiskit/qiskit into expr-float
kevinhartman Feb 28, 2025
481700a
Merge branch 'expr-float' into expr-timing
kevinhartman Feb 28, 2025
e09762f
Special-case Var const-ness for Stretch type.
kevinhartman Feb 28, 2025
cbd55db
Address review comments.
kevinhartman Feb 28, 2025
f12afb4
Update release note.
kevinhartman Feb 28, 2025
8d059cd
Merge branch 'expr-float' into expr-timing
kevinhartman Feb 28, 2025
e05d9ff
Update docs.
kevinhartman Feb 28, 2025
0f14cca
Address review comments.
kevinhartman Mar 1, 2025
7db8222
Merge branch 'expr-float' into expr-timing
kevinhartman Mar 1, 2025
34e615b
Merge branch 'main' of github.com:Qiskit/qiskit into expr-timing
kevinhartman Mar 1, 2025
bbe67a6
Remove Stretch type.
kevinhartman Mar 2, 2025
0acc450
Remove a few more mentions of the binned stretch type.
kevinhartman Mar 2, 2025
7fea216
Add docstring for Duration.
kevinhartman Mar 2, 2025
78b2951
Address review comments.
kevinhartman Mar 3, 2025
97bbeba
Remove unused import.
kevinhartman Mar 3, 2025
49f2af8
Remove visitor short circuit.
kevinhartman Mar 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions qiskit/circuit/classical/expr/visitors.py
Original file line number Diff line number Diff line change
@@ -299,8 +299,4 @@ def is_lvalue(node: expr.Expr, /) -> bool:
>>> expr.is_lvalue(expr.bit_and(a, b))
False
"""
if node.type.const:
# If the expression's resolution type is const, then this can never be
# an l-value (even if the expression is a Var).
return False
return node.accept(_IS_LVALUE)
45 changes: 28 additions & 17 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
@@ -2843,13 +2843,14 @@ def add_var(self, name_or_var: str | expr.Var, /, initial: typing.Any) -> expr.V

Args:
name_or_var: either a string of the variable name, or an existing instance of
:class:`~.expr.Var` to re-use. Variables cannot shadow names that are already in
use within the circuit.
a non-const-typed :class:`~.expr.Var` to re-use. Variables cannot shadow names
that are already in use within the circuit.
initial: the value to initialize this variable with. If the first argument was given
as a string name, the type of the resulting variable is inferred from the initial
expression; to control this more manually, either use :meth:`.Var.new` to manually
construct a new variable with the desired type, or use :func:`.expr.cast` to cast
the initializer to the desired type.
the initializer to the desired type. If a const-typed expression is provided, it
will be automatically cast to its non-const counterpart.

This must be either a :class:`~.expr.Expr` node, or a value that can be lifted to
one using :class:`.expr.lift`.
@@ -2859,7 +2860,8 @@ def add_var(self, name_or_var: str | expr.Var, /, initial: typing.Any) -> expr.V
object will be returned.

Raises:
CircuitError: if the variable cannot be created due to shadowing an existing variable.
CircuitError: if the variable cannot be created due to shadowing an existing variable
or a const variable was specified for ``name_or_var``.

Examples:
Define a new variable given just a name and an initializer expression::
@@ -2900,17 +2902,18 @@ def add_var(self, name_or_var: str | expr.Var, /, initial: typing.Any) -> expr.V
# Validate the initializer first to catch cases where the variable to be declared is being
# used in the initializer.
circuit_scope = self._current_scope()
# Convenience method to widen Python integer literals to the right width during the initial
# lift, if the type is already known via the variable.
if (
isinstance(name_or_var, expr.Var)
and name_or_var.type.kind is types.Uint
and isinstance(initial, int)
and not isinstance(initial, bool)
):
coerce_type = name_or_var.type
else:
coerce_type = None
coerce_type = None
if isinstance(name_or_var, expr.Var):
if name_or_var.type.const:
raise CircuitError("const variables are not supported.")
if (
name_or_var.type.kind is types.Uint
and isinstance(initial, int)
and not isinstance(initial, bool)
):
# Convenience method to widen Python integer literals to the right width during
# the initial lift, if the type is already known via the variable.
coerce_type = name_or_var.type
initial = _validate_expr(circuit_scope, expr.lift(initial, coerce_type))
if isinstance(name_or_var, str):
var = expr.Var.new(name_or_var, initial.type)
@@ -2961,6 +2964,8 @@ def add_uninitialized_var(self, var: expr.Var, /):
raise CircuitError("cannot add an uninitialized variable in a control-flow scope")
if not var.standalone:
raise CircuitError("cannot add a variable wrapping a bit or register to a circuit")
if var.type.const:
raise CircuitError("const variables are not supported.")
self._builder_api.add_uninitialized_var(var)

def add_capture(self, var: expr.Var):
@@ -3023,8 +3028,14 @@ def add_input( # pylint: disable=missing-raises-doc
raise CircuitError("cannot add an input variable in a control-flow scope")
if self._vars_capture:
raise CircuitError("circuits to be enclosed with captures cannot have input variables")
if isinstance(name_or_var, expr.Var) and type_ is not None:
raise ValueError("cannot give an explicit type with an existing Var")
if isinstance(name_or_var, expr.Var):
if type_ is not None:
raise ValueError("cannot give an explicit type with an existing Var")
if name_or_var.type.const:
raise CircuitError("const variables are not supported")
elif type_ is not None and type_.const:
raise CircuitError("const variables are not supported")

var = self._prepare_new_var(name_or_var, type_)
self._vars_input[var.name] = var
return var
24 changes: 24 additions & 0 deletions qiskit/qpy/__init__.py
Original file line number Diff line number Diff line change
@@ -370,6 +370,30 @@ def open(*args):
by ``num_circuits`` in the file header). There is no padding between the
circuits in the data.

.. _qpy_version_14:

Version 14
----------

Version 14 adds support for additional :class:`~.types.Type` classes, and adds support for
const-ness to existing types :class:`~.types.Bool` and :class:`~.types.Uint`.
The ``EXPR_TYPE_BOOL`` and ``EXPR_TYPE_UNIT`` structs are now replaced by ``EXPR_TYPE_BOOL_V14``
and ``EXPR_TYPE_UINT_V14``, respectively. See the updated expression type table below.

EXPR_TYPE
~~~~~~~~~

A :class:`~.types.Type` is encoded by a single-byte ASCII ``char`` that encodes the kind of type,
followed by a payload that varies depending on the type. The defined codes are:

====================== ========= =================================================================
Qiskit class Type code Payload
====================== ========= =================================================================
:class:`~.types.Bool` ``b`` One `_Bool const`.

:class:`~.types.Uint` ``u`` One ``uint32_t width``, followed by one ``_Bool const``.
====================== ========= =================================================================

.. _qpy_version_13:

Version 13
4 changes: 2 additions & 2 deletions qiskit/qpy/binary_io/circuits.py
Original file line number Diff line number Diff line change
@@ -1257,7 +1257,7 @@ def write_circuit(
file_obj.write(metadata_raw)
# Write header payload
file_obj.write(registers_raw)
standalone_var_indices = value.write_standalone_vars(file_obj, circuit)
standalone_var_indices = value.write_standalone_vars(file_obj, circuit, version)
else:
if circuit.num_vars:
raise exceptions.UnsupportedFeatureForVersion(
@@ -1425,7 +1425,7 @@ def read_circuit(file_obj, version, metadata_deserializer=None, use_symengine=Fa
"q": [Qubit() for _ in out_bits["q"]],
"c": [Clbit() for _ in out_bits["c"]],
}
var_segments, standalone_var_indices = value.read_standalone_vars(file_obj, num_vars)
var_segments, standalone_var_indices = value.read_standalone_vars(file_obj, num_vars, version)
circ = QuantumCircuit(
out_bits["q"],
out_bits["c"],
Loading