Skip to content

Commit

Permalink
Address review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhartman committed Feb 24, 2025
1 parent 50deb93 commit f1dc1a1
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 12 deletions.
9 changes: 8 additions & 1 deletion qiskit/circuit/classical/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
.. autoclass:: Bool
.. autoclass:: Uint
All types have a :attr:`~Type.const` field to indicate their const-ness. When the result type of
an expression is constant, the expression is considered a constant expression. Constant
expressions can be used in certain contexts that aren't valid for runtime-initialized variables.
Note that :class:`Uint` defines a family of types parametrized by their width; it is not one single
type, which may be slightly different to the 'classical' programming languages you are used to.
Expand All @@ -59,7 +63,10 @@
The type system is equipped with a partial ordering, where :math:`a < b` is interpreted as
":math:`a` is a strict subtype of :math:`b`". Note that the partial ordering is a subset of the
directed graph that describes the allowed explicit casting operations between types. The partial
ordering defines when one type may be lossless directly interpreted as another.
ordering defines when one type may be losslessly directly interpreted as another.
When two types differ only in const-ness, the non-const version is considered to be the
"greater" of the two.
The low-level interface to querying the subtyping relationship is the :func:`order` function.
Expand Down
6 changes: 3 additions & 3 deletions qiskit/circuit/classical/types/ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ def order(left: Type, right: Type, /) -> Ordering:
# If one type is greater (and thus is the only type that can represent
# both) an ordering is only defined if that type is non-const or both
# types are const.
if left.const is True and right.const is False:
if left.const and not right.const:
if order_ is Ordering.EQUAL:
return Ordering.LESS
if order_ is Ordering.GREATER:
return Ordering.NONE
if right.const is True and left.const is False:
if right.const and not left.const:
if order_ is Ordering.EQUAL:
return Ordering.GREATER
if order_ is Ordering.LESS:
Expand Down Expand Up @@ -251,7 +251,7 @@ def cast_kind(from_: Type, to_: Type, /) -> CastKind:
>>> types.cast_kind(types.Uint(16), types.Uint(8))
<CastKind.DANGEROUS: 4>
"""
if to_.const is True and from_.const is False:
if to_.const and not from_.const:
# We can't cast to a const type.
return CastKind.NONE
if (coercer := _ALLOWED_CASTS.get((from_.kind, to_.kind))) is None:
Expand Down
4 changes: 2 additions & 2 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2940,9 +2940,9 @@ def add_input( # pylint: disable=missing-raises-doc
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")
raise CircuitError("const variables cannot be input variables")
elif type_ is not None and type_.const:
raise CircuitError("const variables are not supported")
raise CircuitError("const variables cannot be input variables")

var = self._prepare_new_var(name_or_var, type_)
self._vars_input[var.name] = var
Expand Down
1 change: 0 additions & 1 deletion qiskit/qpy/binary_io/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ def _write_expr_type_v14(file_obj, type_: types.Type):
elif type_.kind is types.Uint:
file_obj.write(type_keys.ExprType.UINT)
file_obj.write(
# TODO: make sure you're calling this correctly
struct.pack(
formats.EXPR_TYPE_UINT_PACK_V14,
*formats.EXPR_TYPE_UINT_V14(type_.width, type_.const),
Expand Down
4 changes: 2 additions & 2 deletions releasenotes/notes/const-expr-397ff09042942b81.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ features_circuits:
from qiskit.circuit.classical import expr
expr.lift(5, try_const=True)
# >>> Value(5, Uint(3, const=True))
# Value(5, Uint(3, const=True))
The result type of an operation applied to const types is also const::
from qiskit.circuit.classical import expr
expr.bit_and(expr.lift(5, try_const=True), expr.lift(6, try_const=True)).type.const
# >>> True
# True
6 changes: 3 additions & 3 deletions test/python/circuit/test_circuit_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_initialise_inputs_declarations(self):

def test_initialise_inputs_declarations_rejects_const_vars(self):
a = expr.Var.new("a", types.Uint(16, const=True))
with self.assertRaisesRegex(CircuitError, "const variables.*not supported"):
with self.assertRaisesRegex(CircuitError, "const variables cannot be input variables"):
QuantumCircuit(inputs=[a])

def test_initialise_captures_declarations(self):
Expand Down Expand Up @@ -219,9 +219,9 @@ def test_add_input_returns_input(self):
def test_add_input_rejects_const_var(self):
a = expr.Var.new("a", types.Bool(const=True))
qc = QuantumCircuit()
with self.assertRaisesRegex(CircuitError, "const variables.*not supported"):
with self.assertRaisesRegex(CircuitError, "const variables cannot be input variables"):
qc.add_input(a)
with self.assertRaisesRegex(CircuitError, "const variables.*not supported"):
with self.assertRaisesRegex(CircuitError, "const variables cannot be input variables"):
qc.add_input("a", types.Bool(const=True))

def test_cannot_have_both_inputs_and_captures(self):
Expand Down

0 comments on commit f1dc1a1

Please sign in to comment.