14
14
15
15
"""Target gateset used for compiling circuits to CZ + 1-q rotations + measurement gates."""
16
16
17
- from typing import Any , Dict , TYPE_CHECKING
17
+ from typing import Any , Dict , Sequence , Type , Union , TYPE_CHECKING
18
18
19
19
from cirq import ops , protocols
20
20
from cirq .transformers .analytical_decompositions import two_qubit_to_cz
25
25
26
26
27
27
class CZTargetGateset (compilation_target_gateset .TwoQubitCompilationTargetGateset ):
28
- """Target gateset containing CZ + single qubit rotations + Measurement gates."""
28
+ """Target gateset accepting CZ + single qubit rotations + measurement gates.
29
29
30
- def __init__ (self , * , atol : float = 1e-8 , allow_partial_czs : bool = False ) -> None :
30
+ By default, `cirq.CZTargetGateset` will accept and compile unknown gates to
31
+ the following universal target gateset:
32
+ - `cirq.CZ` / `cirq.CZPowGate`: The two qubit entangling gate.
33
+ - `cirq.PhasedXZGate`: Single qubit rotations.
34
+ - `cirq.MeasurementGate`: Measurements.
35
+ - `cirq.GlobalPhaseGate`: Global phase.
36
+
37
+ Optionally, users can also specify additional gates / gate families which should
38
+ be accepted by this gateset via the `additional_gates` argument.
39
+
40
+ When compiling a circuit, any unknown gate, i.e. a gate which is not accepted by
41
+ this gateset, will be compiled to the default gateset (i.e. `cirq.CZ`/`cirq.CZPowGate`,
42
+ `cirq.PhasedXZGate`, `cirq.MeasurementGate`).
43
+ """
44
+
45
+ def __init__ (
46
+ self ,
47
+ * ,
48
+ atol : float = 1e-8 ,
49
+ allow_partial_czs : bool = False ,
50
+ additional_gates : Sequence [Union [Type ['cirq.Gate' ], 'cirq.Gate' , 'cirq.GateFamily' ]] = (),
51
+ ) -> None :
31
52
"""Initializes CZTargetGateset
32
53
33
54
Args:
34
55
atol: A limit on the amount of absolute error introduced by the decomposition.
35
56
allow_partial_czs: If set, all powers of the form `cirq.CZ**t`, and not just
36
57
`cirq.CZ`, are part of this gateset.
58
+ additional_gates: Sequence of additional gates / gate families which should also
59
+ be "accepted" by this gateset. Defaults to `cirq.GlobalPhaseGate`.
37
60
"""
38
61
super ().__init__ (
39
62
ops .CZPowGate if allow_partial_czs else ops .CZ ,
40
63
ops .MeasurementGate ,
41
- ops .AnyUnitaryGateFamily ( 1 ) ,
64
+ ops .PhasedXZGate ,
42
65
ops .GlobalPhaseGate ,
66
+ * additional_gates ,
43
67
name = 'CZPowTargetGateset' if allow_partial_czs else 'CZTargetGateset' ,
44
68
)
69
+ self .additional_gates = tuple (
70
+ g if isinstance (g , ops .GateFamily ) else ops .GateFamily (gate = g ) for g in additional_gates
71
+ )
72
+ self ._additional_gates_repr_str = ", " .join (
73
+ [ops .gateset ._gate_str (g , repr ) for g in additional_gates ]
74
+ )
45
75
self .atol = atol
46
76
self .allow_partial_czs = allow_partial_czs
47
77
@@ -57,14 +87,25 @@ def _decompose_two_qubit_operation(self, op: 'cirq.Operation', _) -> 'cirq.OP_TR
57
87
)
58
88
59
89
def __repr__ (self ) -> str :
60
- return f'cirq.CZTargetGateset(atol={ self .atol } , allow_partial_czs={ self .allow_partial_czs } )'
90
+ return (
91
+ f'cirq.CZTargetGateset('
92
+ f'atol={ self .atol } , '
93
+ f'allow_partial_czs={ self .allow_partial_czs } , '
94
+ f'additional_gates=[{ self ._additional_gates_repr_str } ]'
95
+ f')'
96
+ )
61
97
62
98
def _value_equality_values_ (self ) -> Any :
63
- return self .atol , self .allow_partial_czs
99
+ return self .atol , self .allow_partial_czs , frozenset ( self . additional_gates )
64
100
65
101
def _json_dict_ (self ) -> Dict [str , Any ]:
66
- return {'atol' : self .atol , 'allow_partial_czs' : self .allow_partial_czs }
102
+ d : Dict [str , Any ] = {'atol' : self .atol , 'allow_partial_czs' : self .allow_partial_czs }
103
+ if self .additional_gates :
104
+ d ['additional_gates' ] = list (self .additional_gates )
105
+ return d
67
106
68
107
@classmethod
69
- def _from_json_dict_ (cls , atol , allow_partial_czs , ** kwargs ):
70
- return cls (atol = atol , allow_partial_czs = allow_partial_czs )
108
+ def _from_json_dict_ (cls , atol , allow_partial_czs , additional_gates = (), ** kwargs ):
109
+ return cls (
110
+ atol = atol , allow_partial_czs = allow_partial_czs , additional_gates = additional_gates
111
+ )
0 commit comments