1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- """Transformer pass that adds dynamical decoupling moments to a circuit."""
15+ """Transformer pass that adds dynamical decoupling operations to a circuit."""
1616
1717import enum
1818from functools import reduce
1919from typing import Any , Dict , Optional , Tuple
2020
21+ from cirq .transformers import transformer_api , transformer_primitives
2122import cirq
2223from cirq import value
2324import numpy as np
2425
26+ # DO NOT SUBMIT
27+ import logging
28+
2529
2630@enum .unique
2731class _DynamicalDecouplingSchema (enum .Enum ):
@@ -41,25 +45,28 @@ def _generate_dd_sequence_from_schema(
4145) -> list ['cirq.Gate' ]:
4246 match schema :
4347 case _DynamicalDecouplingSchema .XX_PAIR :
44- return _repeat_sequence ([cirq .XPowGate () , cirq .XPowGate () ], num_idle_moments )
48+ return _repeat_sequence ([cirq .X , cirq .X ], num_idle_moments )
4549 case _DynamicalDecouplingSchema .YY_PAIR :
46- return _repeat_sequence ([cirq .YPowGate () , cirq .YPowGate () ], num_idle_moments )
50+ return _repeat_sequence ([cirq .Y , cirq .Y ], num_idle_moments )
4751
4852
4953def _validate_dd_sequence (dd_sequence : list ['cirq.Gate' ]) -> None :
5054 if len (dd_sequence ) < 2 :
5155 raise ValueError ('Invalid dynamical decoupling sequence. Expect more than one gates.' )
5256 matrices = [cirq .unitary (gate ) for gate in dd_sequence ]
5357 product = reduce (np .matmul , matrices )
54- if not np .array_equal (product , np .eye (2 )):
58+ product_gate = cirq .MatrixGate (product )
59+
60+ a = cirq .NamedQubit ("a" )
61+ if cirq .equal_up_to_global_phase (product_gate (a ), cirq .I (a )):
5562 raise ValueError (
56- "Invalid dynamical decoupling sequence, sequence product doesn't equal" ' identity.'
63+ "Invalid dynamical decoupling sequence. Sequence should equal to identity operation up to a global phase."
5764 )
5865
5966
6067@value .value_equality
6168class DynamicalDecouplingModel :
62- """Dynamical decoupling model that generates dynamical decoupling gate sequences."""
69+ """Dynamical decoupling model that generates dynamical decoupling operation sequences."""
6370
6471 def __init__ (
6572 self ,
@@ -84,7 +91,6 @@ def generate_dd_sequence(self, num_idle_moments: int = 2) -> list['cirq.Gate']:
8491 return _generate_dd_sequence_from_schema (self .schema , num_idle_moments )
8592 if self .base_dd_sequence :
8693 return _repeat_sequence (self .base_dd_sequence , num_idle_moments )
87- return []
8894
8995 @classmethod
9096 def from_schema (cls , schema : str ):
@@ -117,18 +123,23 @@ def _value_equality_values_(self) -> Any:
117123 return self .schema , self .base_dd_sequence
118124
119125
126+ @transformer_api .transformer (add_deep_support = True )
120127def add_dynamical_decoupling (
121- circuit : 'cirq.AbstractCircuit' , dd_model : DynamicalDecouplingModel
128+ circuit : 'cirq.AbstractCircuit' ,
129+ dd_model : DynamicalDecouplingModel ,
130+ * ,
131+ context : Optional ['cirq.TransformerContext' ] = None ,
122132) -> 'cirq.Circuit' :
123133 """Add dynamical decoupling gates in a given circuit.
124134
125135 Args:
126136 circuit: Input circuit to transform.
137+ context: `cirq.TransformerContext` storing common configurable options for transformers.
127138 dd_model: Dynamical decoupling model that defines the schema to generate
128139 dynamical decoupling sequences.
129140
130141 Return:
131- A circuit with dynamical decoupling operations.
142+ A copy of the input circuit with dynamical decoupling operations.
132143 """
133144 last_busy_moment_by_qubits : Dict ['cirq.Qid' , int ] = {q : 0 for q in circuit .all_qubits ()}
134145 insert_into : list [Tuple [int , 'cirq.OP_TREE' ]] = []
@@ -141,6 +152,7 @@ def add_dynamical_decoupling(
141152 insert_into .append ((last_busy_moment_by_qubits [q ] + idx + 1 , gate .on (q )))
142153 last_busy_moment_by_qubits [q ] = moment_id
143154
144- circuit .batch_insert_into (insert_into )
155+ updated_circuit = circuit .unfreeze (copy = True )
156+ updated_circuit .batch_insert_into (insert_into )
145157
146- return circuit
158+ return updated_circuit
0 commit comments