@@ -136,11 +136,17 @@ def assert_connections_compatible(cbloq: CompositeBloq):
136136 raise BloqError (f"{ cxn } 's right side is associated with a register with side { rr .side } " )
137137
138138
139- def assert_connections_preserve_types (
139+ def assert_connections_consistent_qdtypes (
140140 cbloq : CompositeBloq ,
141141 type_checking_severity : QDTypeCheckingSeverity = QDTypeCheckingSeverity .LOOSE ,
142142):
143- """Check that connections have consistent dtypes accounting for type_checking_severity."""
143+ """Check that a composite bloq's connections have consistent qdtypes.
144+
145+ Args:
146+ cbloq: The composite bloq.
147+ type_checking_severity: How strict to be in type checking. See the documentation
148+ for the QDTypeCheckingSeverity enum for details.
149+ """
144150 for cxn in cbloq .connections :
145151 lr = cxn .left .reg
146152 rr = cxn .right .reg
@@ -525,7 +531,23 @@ def check_equivalent_bloq_example_counts(bloq_ex: BloqExample) -> Tuple[BloqChec
525531 return BloqCheckResult .PASS , ''
526532
527533
528- def assert_bloq_example_serialize (bloq_ex : BloqExample ) -> Tuple [BloqCheckResult , str ]:
534+ def assert_bloq_example_serializes (bloq_ex : BloqExample ) -> None :
535+ """Assert that the BloqExample has consistent serialization.
536+
537+ This function asserts that the given bloq can be serialized to a proto format and the
538+ corresponding proto can be deserialized back to a bloq which is equal to the original
539+ bloq.
540+
541+ If the given Bloq cannot be serialized / deserialized OR if the deserialized Bloq is not
542+ equal to the given Bloq, then the result is `FAIL`. If the roundtrip succeeds, the result
543+ is `PASS`.
544+
545+ Returns:
546+ None if the assertions are satisfied.
547+
548+ Raises:
549+ BloqCheckException if any assertions are violated.
550+ """
529551 from qualtran .serialization .bloq import bloqs_from_proto , bloqs_to_proto
530552
531553 bloq = bloq_ex .make ()
@@ -547,7 +569,7 @@ def assert_bloq_example_serialize(bloq_ex: BloqExample) -> Tuple[BloqCheckResult
547569 ) from e
548570
549571
550- def check_bloq_example_serialize (bloq_ex : BloqExample ) -> Tuple [BloqCheckResult , str ]:
572+ def check_bloq_example_serializes (bloq_ex : BloqExample ) -> Tuple [BloqCheckResult , str ]:
551573 """Check that the BloqExample has consistent serialization.
552574
553575 This function checks that the given bloq can be serialized to a proto format and the
@@ -563,7 +585,7 @@ def check_bloq_example_serialize(bloq_ex: BloqExample) -> Tuple[BloqCheckResult,
563585 msg: A message providing details from the check.
564586 """
565587 try :
566- assert_bloq_example_serialize (bloq_ex )
588+ assert_bloq_example_serializes (bloq_ex )
567589 except BloqCheckException as bce :
568590 return bce .check_result , bce .msg
569591 except Exception as e : # pylint: disable=broad-except
@@ -572,55 +594,73 @@ def check_bloq_example_serialize(bloq_ex: BloqExample) -> Tuple[BloqCheckResult,
572594 return BloqCheckResult .PASS , ''
573595
574596
575- def assert_bloq_example_preserves_types (bloq_ex : BloqExample ) -> Tuple [BloqCheckResult , str ]:
576- """Check a bloq example preserves types throughout its decomposition.
597+ def assert_bloq_example_qtyping (bloq_ex : BloqExample ) -> Tuple [BloqCheckResult , str ]:
598+ """Assert that the bloq example has valid quantum data types throughout its decomposition.
577599
578- Args:
579- bloq_ex: The bloq example to test.
600+ If the bloq has no decomposition, this check is not applicable. Otherwise: we check the
601+ `connections` in the decomposed bloq with increasing levels of type checking severity.
602+ First, we check loose type checking (allowing conversions between numeric types). A
603+ failure here is raised as a FAIL.
604+
605+ Then `QDTypeCheckingSeverity.ANY` checking (allowing just conversions to and from QAny) and
606+ finally strict checking are performed. Currently, these are coded as an UNVERIFIED bloq
607+ check result.
580608
581609 Returns:
582- None
610+ None if the assertions are satisfied.
583611
584612 Raises:
585- BloqCheckException if any assertions are violated. Will raise a failure
586- if loose type checking fails, and unverified if the stricter type checks
587- fail.
613+ BloqCheckException if any assertions are violated.
588614 """
589615 bloq = bloq_ex .make ()
590- # First check it's not atomic / doesn't decompose
616+ # First check for the presence of a decomposition
591617 try :
592618 cbloq = bloq .decompose_bloq ()
593- except (DecomposeTypeError , DecomposeNotImplementedError ) as exc :
594- raise BloqCheckException .missing (
595- r"Atomic bloqs or non-decomposable bloqs don't require type checking."
596- )
619+ except (DecomposeTypeError , DecomposeNotImplementedError ) as e :
620+ raise BloqCheckException .na (
621+ r"QDType checking is only applicable for bloqs with decompositions"
622+ ) from e
623+
597624 try :
598- assert_connections_preserve_types (
625+ assert_connections_consistent_qdtypes (
599626 cbloq , type_checking_severity = QDTypeCheckingSeverity .LOOSE
600627 )
601628 except Exception as e :
602629 raise BloqCheckException .fail ('Loose type checking failed.\n ' + str (e )) from e
630+
603631 try :
604- assert_connections_preserve_types (cbloq , type_checking_severity = QDTypeCheckingSeverity .ANY )
632+ assert_connections_consistent_qdtypes (
633+ cbloq , type_checking_severity = QDTypeCheckingSeverity .ANY
634+ )
605635 except Exception as e :
606636 raise BloqCheckException .unverified ('QAny and QBit type checking failed.\n ' + str (e )) from e
637+
607638 try :
608- assert_connections_preserve_types (
639+ assert_connections_consistent_qdtypes (
609640 cbloq , type_checking_severity = QDTypeCheckingSeverity .STRICT
610641 )
611642 except Exception as e :
612643 raise BloqCheckException .unverified ('Strict type checking failed.\n ' + str (e )) from e
613644
614645
615- def check_connections_preserve_preserves_types (bloq_ex : BloqExample ) -> Tuple [BloqCheckResult , str ]:
616- """Check that the BloqExample has consistent typing.
646+ def check_bloq_example_qtyping (bloq_ex : BloqExample ) -> Tuple [BloqCheckResult , str ]:
647+ """Check that the bloq example has valid quantum data types throughout its decomposition.
648+
649+ If the bloq has no decomposition, this check is not applicable. Otherwise: we check the
650+ `connections` in the decomposed bloq with increasing levels of type checking severity.
651+ First, we check loose type checking (allowing conversions between numeric types). A
652+ failure here is returned as a FAIL.
653+
654+ Then `QDTypeCheckingSeverity.ANY` checking (allowing just conversions to and from QAny) and
655+ finally strict checking are performed. Currently, these are coded as an UNVERIFIED bloq
656+ check result.
617657
618658 Returns:
619659 result: The `BloqCheckResult`.
620660 msg: A message providing details from the check.
621661 """
622662 try :
623- assert_bloq_example_preserves_types (bloq_ex )
663+ assert_bloq_example_qtyping (bloq_ex )
624664 except BloqCheckException as bce :
625665 return bce .check_result , bce .msg
626666 except Exception as e : # pylint: disable=broad-except
0 commit comments