1919 AbstractSet ,
2020 Any ,
2121 Callable ,
22+ cast ,
2223 Dict ,
2324 FrozenSet ,
2425 Iterable ,
@@ -80,17 +81,26 @@ class Moment:
8081 are no such operations, returns an empty Moment.
8182 """
8283
83- def __init__ (self , * contents : 'cirq.OP_TREE' ) -> None :
84+ def __init__ (self , * contents : 'cirq.OP_TREE' , _flatten_contents : bool = True ) -> None :
8485 """Constructs a moment with the given operations.
8586
8687 Args:
8788 contents: The operations applied within the moment.
8889 Will be flattened and frozen into a tuple before storing.
90+ _flatten_contents: If True, use flatten_to_ops to convert
91+ the OP_TREE of contents into a tuple of Operation. If False,
92+ we skip flattening and assume that contents already consists
93+ of individual operations. This is used internally by helper
94+ methods to avoid unnecessary validation.
8995
9096 Raises:
9197 ValueError: A qubit appears more than once.
9298 """
93- self ._operations = tuple (op_tree .flatten_to_ops (contents ))
99+ self ._operations = (
100+ tuple (op_tree .flatten_to_ops (contents ))
101+ if _flatten_contents
102+ else cast (Tuple ['cirq.Operation' ], contents )
103+ )
94104 self ._sorted_operations : Optional [Tuple ['cirq.Operation' , ...]] = None
95105
96106 # An internal dictionary to support efficient operation access by qubit.
@@ -106,6 +116,10 @@ def __init__(self, *contents: 'cirq.OP_TREE') -> None:
106116 self ._measurement_key_objs : Optional [FrozenSet ['cirq.MeasurementKey' ]] = None
107117 self ._control_keys : Optional [FrozenSet ['cirq.MeasurementKey' ]] = None
108118
119+ @classmethod
120+ def from_ops (cls , * ops : 'cirq.Operation' ) -> 'cirq.Moment' :
121+ return cls (* ops , _flatten_contents = False )
122+
109123 @property
110124 def operations (self ) -> Tuple ['cirq.Operation' , ...]:
111125 return self ._operations
0 commit comments