-
Notifications
You must be signed in to change notification settings - Fork 982
/
merge_single_qubit_gates.py
154 lines (129 loc) · 5.69 KB
/
merge_single_qubit_gates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Copyright 2022 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Transformer passes to combine adjacent single-qubit rotations."""
from typing import Optional, TYPE_CHECKING
from cirq import circuits, ops, protocols
from cirq.transformers.analytical_decompositions import single_qubit_decompositions
from cirq.transformers import transformer_api, transformer_primitives, merge_k_qubit_gates
if TYPE_CHECKING:
import cirq
@transformer_api.transformer
def merge_single_qubit_gates_to_phased_x_and_z(
circuit: 'cirq.AbstractCircuit',
*,
context: Optional['cirq.TransformerContext'] = None,
atol: float = 1e-8,
) -> 'cirq.Circuit':
"""Replaces runs of single qubit rotations with `cirq.PhasedXPowGate` and `cirq.ZPowGate`.
Specifically, any run of non-parameterized single-qubit unitaries will be replaced by an
optional PhasedX operation followed by an optional Z operation.
Args:
circuit: Input circuit to transform. It will not be modified.
context: `cirq.TransformerContext` storing common configurable options for transformers.
atol: Absolute tolerance to angle error. Larger values allow more negligible gates to be
dropped, smaller values increase accuracy.
Returns:
Copy of the transformed input circuit.
"""
def rewriter(op: 'cirq.CircuitOperation') -> 'cirq.OP_TREE':
u = protocols.unitary(op)
if protocols.num_qubits(op) == 0:
return ops.GlobalPhaseGate(u[0, 0]).on()
return [
g(op.qubits[0])
for g in single_qubit_decompositions.single_qubit_matrix_to_phased_x_z(u, atol)
]
return merge_k_qubit_gates.merge_k_qubit_unitaries(
circuit, k=1, context=context, rewriter=rewriter
)
@transformer_api.transformer
def merge_single_qubit_gates_to_phxz(
circuit: 'cirq.AbstractCircuit',
*,
context: Optional['cirq.TransformerContext'] = None,
atol: float = 1e-8,
) -> 'cirq.Circuit':
"""Replaces runs of single qubit rotations with a single optional `cirq.PhasedXZGate`.
Specifically, any run of non-parameterized single-qubit unitaries will be replaced by an
optional PhasedXZ.
Args:
circuit: Input circuit to transform. It will not be modified.
context: `cirq.TransformerContext` storing common configurable options for transformers.
atol: Absolute tolerance to angle error. Larger values allow more negligible gates to be
dropped, smaller values increase accuracy.
Returns:
Copy of the transformed input circuit.
"""
def rewriter(op: 'cirq.CircuitOperation') -> 'cirq.OP_TREE':
u = protocols.unitary(op)
if protocols.num_qubits(op) == 0:
return ops.GlobalPhaseGate(u[0, 0]).on()
gate = single_qubit_decompositions.single_qubit_matrix_to_phxz(u, atol)
return gate(op.qubits[0]) if gate else []
return merge_k_qubit_gates.merge_k_qubit_unitaries(
circuit, k=1, context=context, rewriter=rewriter
)
@transformer_api.transformer
def merge_single_qubit_moments_to_phxz(
circuit: 'cirq.AbstractCircuit',
*,
context: Optional['cirq.TransformerContext'] = None,
atol: float = 1e-8,
) -> 'cirq.Circuit':
"""Merges adjacent moments with only 1-qubit rotations to a single moment with PhasedXZ gates.
Args:
circuit: Input circuit to transform. It will not be modified.
context: `cirq.TransformerContext` storing common configurable options for transformers.
atol: Absolute tolerance to angle error. Larger values allow more negligible gates to be
dropped, smaller values increase accuracy.
Returns:
Copy of the transformed input circuit.
"""
tags_to_ignore = set(context.tags_to_ignore) if context else set()
def can_merge_moment(m: 'cirq.Moment'):
return all(
protocols.num_qubits(op) == 1
and protocols.has_unitary(op)
and tags_to_ignore.isdisjoint(op.tags)
for op in m
)
def merge_func(m1: 'cirq.Moment', m2: 'cirq.Moment') -> Optional['cirq.Moment']:
if not (can_merge_moment(m1) and can_merge_moment(m2)):
return None
ret_ops = []
for q in m1.qubits | m2.qubits:
op1, op2 = m1.operation_at(q), m2.operation_at(q)
if op1 and op2:
mat = protocols.unitary(op2) @ protocols.unitary(op1)
gate = single_qubit_decompositions.single_qubit_matrix_to_phxz(mat, atol)
if gate:
ret_ops.append(gate(q))
else:
op = op1 or op2
assert op is not None
if isinstance(op.gate, ops.PhasedXZGate):
ret_ops.append(op)
else:
gate = single_qubit_decompositions.single_qubit_matrix_to_phxz(
protocols.unitary(op), atol
)
if gate:
ret_ops.append(gate(q))
return circuits.Moment(ret_ops)
return transformer_primitives.merge_moments(
circuit,
merge_func,
deep=context.deep if context else False,
tags_to_ignore=tuple(tags_to_ignore),
).unfreeze(copy=False)