-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
cnotdihedral_decompose_two_qubits.py
266 lines (244 loc) · 8.58 KB
/
cnotdihedral_decompose_two_qubits.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# This code is part of Qiskit.
#
# (C) Copyright IBM 2019, 2024.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
Circuit synthesis for the CNOTDihedral class.
"""
import numpy as np
from qiskit.circuit import QuantumCircuit
from qiskit.exceptions import QiskitError
from qiskit.quantum_info import CNOTDihedral
def synth_cnotdihedral_two_qubits(elem: CNOTDihedral) -> QuantumCircuit:
"""Decompose a :class:`.CNOTDihedral` element on a single qubit and two
qubits into a :class:`.QuantumCircuit`.
This decomposition has an optimal number of :class:`.CXGate`\\ s.
Args:
elem: A :class:`.CNOTDihedral` element.
Returns:
A circuit implementation of the :class:`.CNOTDihedral` element.
Raises:
QiskitError: if the element in not 1-qubit or 2-qubit :class:`.CNOTDihedral`.
References:
1. Shelly Garion and Andrew W. Cross, *On the structure of the CNOT-Dihedral group*,
`arXiv:2006.12042 [quant-ph] <https://arxiv.org/abs/2006.12042>`_
"""
circuit = QuantumCircuit(elem.num_qubits)
if elem.num_qubits > 2:
raise QiskitError(
"Cannot decompose a CNOT-Dihedral element with more than 2 qubits. "
"use synth_cnotdihedral_full function instead."
)
if elem.num_qubits == 1:
if elem.poly.weight_0 != 0 or elem.linear != [[1]]:
raise QiskitError("1-qubit element in not CNOT-Dihedral .")
tpow0 = elem.poly.weight_1[0]
xpow0 = elem.shift[0]
if tpow0 > 0:
circuit.p((tpow0 * np.pi / 4), [0])
if xpow0 == 1:
circuit.x(0)
if tpow0 == 0 and xpow0 == 0:
circuit.id(0)
return circuit
# case elem.num_qubits == 2:
if elem.poly.weight_0 != 0:
raise QiskitError("2-qubit element in not CNOT-Dihedral .")
weight_1 = elem.poly.weight_1
weight_2 = elem.poly.weight_2
linear = elem.linear
shift = elem.shift
# CS subgroup
if (linear == [[1, 0], [0, 1]]).all():
[xpow0, xpow1] = shift
# Dihedral class
if weight_2 == [0]:
[tpow0, tpow1] = weight_1
if tpow0 > 0:
circuit.p((tpow0 * np.pi / 4), [0])
if xpow0 == 1:
circuit.x(0)
if tpow1 > 0:
circuit.p((tpow1 * np.pi / 4), [1])
if xpow1 == 1:
circuit.x(1)
if tpow0 == 0 and tpow1 == 0 and xpow0 == 0 and xpow1 == 0:
circuit.id(0)
circuit.id(1)
# CS-like class
if (weight_2 == [2] and xpow0 == xpow1) or (weight_2 == [6] and xpow0 != xpow1):
tpow0 = (weight_1[0] - 2 * xpow1 - 4 * xpow0 * xpow1) % 8
tpow1 = (weight_1[1] - 2 * xpow0 - 4 * xpow0 * xpow1) % 8
if tpow0 > 0:
circuit.p((tpow0 * np.pi / 4), [0])
if xpow0 == 1:
circuit.x(0)
if tpow1 > 0:
circuit.p((tpow1 * np.pi / 4), [1])
if xpow1 == 1:
circuit.x(1)
# CS gate is implemented using 2 CX gates
circuit.p((np.pi / 4), [0])
circuit.p((np.pi / 4), [1])
circuit.cx(0, 1)
circuit.p((7 * np.pi / 4), [1])
circuit.cx(0, 1)
# CSdg-like class
if (weight_2 == [6] and xpow0 == xpow1) or (weight_2 == [2] and xpow0 != xpow1):
tpow0 = (weight_1[0] - 6 * xpow1 - 4 * xpow0 * xpow1) % 8
tpow1 = (weight_1[1] - 6 * xpow0 - 4 * xpow0 * xpow1) % 8
if tpow0 > 0:
circuit.p((tpow0 * np.pi / 4), [0])
if xpow0 == 1:
circuit.x(0)
if tpow1 > 0:
circuit.p((tpow1 * np.pi / 4), [1])
if xpow1 == 1:
circuit.x(1)
# CSdg gate is implemented using 2 CX gates
circuit.p((7 * np.pi / 4), [0])
circuit.p((7 * np.pi / 4), [1])
circuit.cx(0, 1)
circuit.p((np.pi / 4), [1])
circuit.cx(0, 1)
# CZ-like class
if weight_2 == [4]:
tpow0 = (weight_1[0] - 4 * xpow1) % 8
tpow1 = (weight_1[1] - 4 * xpow0) % 8
if tpow0 > 0:
circuit.p((tpow0 * np.pi / 4), [0])
if xpow0 == 1:
circuit.x(0)
if tpow1 > 0:
circuit.p((tpow1 * np.pi / 4), [1])
if xpow1 == 1:
circuit.x(1)
# CZ gate is implemented using 2 CX gates
circuit.cz(1, 0)
# CX01-like class
if (linear == [[1, 0], [1, 1]]).all():
xpow0 = shift[0]
xpow1 = (shift[1] + xpow0) % 2
if xpow0 == xpow1:
m = ((8 - weight_2[0]) / 2) % 4
tpow0 = (weight_1[0] - m) % 8
tpow1 = (weight_1[1] - m) % 8
else:
m = (weight_2[0] / 2) % 4
tpow0 = (weight_1[0] + m) % 8
tpow1 = (weight_1[1] + m) % 8
if tpow0 > 0:
circuit.p((tpow0 * np.pi / 4), [0])
if xpow0 == 1:
circuit.x(0)
if tpow1 > 0:
circuit.p((tpow1 * np.pi / 4), [1])
if xpow1 == 1:
circuit.x(1)
circuit.cx(0, 1)
if m > 0:
circuit.p((m * np.pi / 4), [1])
# CX10-like class
if (linear == [[1, 1], [0, 1]]).all():
xpow1 = shift[1]
xpow0 = (shift[0] + xpow1) % 2
if xpow0 == xpow1:
m = ((8 - weight_2[0]) / 2) % 4
tpow0 = (weight_1[0] - m) % 8
tpow1 = (weight_1[1] - m) % 8
else:
m = (weight_2[0] / 2) % 4
tpow0 = (weight_1[0] + m) % 8
tpow1 = (weight_1[1] + m) % 8
if tpow0 > 0:
circuit.p((tpow0 * np.pi / 4), [0])
if xpow0 == 1:
circuit.x(0)
if tpow1 > 0:
circuit.p((tpow1 * np.pi / 4), [1])
if xpow1 == 1:
circuit.x(1)
circuit.cx(1, 0)
if m > 0:
circuit.p((m * np.pi / 4), [0])
# CX01*CX10-like class
if (linear == [[0, 1], [1, 1]]).all():
xpow1 = shift[0]
xpow0 = (shift[1] + xpow1) % 2
if xpow0 == xpow1:
m = ((8 - weight_2[0]) / 2) % 4
tpow0 = (weight_1[0] - m) % 8
tpow1 = (weight_1[1] - m) % 8
else:
m = (weight_2[0] / 2) % 4
tpow0 = (weight_1[0] + m) % 8
tpow1 = (weight_1[1] + m) % 8
if tpow0 > 0:
circuit.p((tpow0 * np.pi / 4), [0])
if xpow0 == 1:
circuit.x(0)
if tpow1 > 0:
circuit.p((tpow1 * np.pi / 4), [1])
if xpow1 == 1:
circuit.x(1)
circuit.cx(0, 1)
circuit.cx(1, 0)
if m > 0:
circuit.p((m * np.pi / 4), [1])
# CX10*CX01-like class
if (linear == [[1, 1], [1, 0]]).all():
xpow0 = shift[1]
xpow1 = (shift[0] + xpow0) % 2
if xpow0 == xpow1:
m = ((8 - weight_2[0]) / 2) % 4
tpow0 = (weight_1[0] - m) % 8
tpow1 = (weight_1[1] - m) % 8
else:
m = (weight_2[0] / 2) % 4
tpow0 = (weight_1[0] + m) % 8
tpow1 = (weight_1[1] + m) % 8
if tpow0 > 0:
circuit.p((tpow0 * np.pi / 4), [0])
if xpow0 == 1:
circuit.x(0)
if tpow1 > 0:
circuit.p((tpow1 * np.pi / 4), [1])
if xpow1 == 1:
circuit.x(1)
circuit.cx(1, 0)
circuit.cx(0, 1)
if m > 0:
circuit.p((m * np.pi / 4), [0])
# CX01*CX10*CX01-like class
if (linear == [[0, 1], [1, 0]]).all():
xpow0 = shift[1]
xpow1 = shift[0]
if xpow0 == xpow1:
m = ((8 - weight_2[0]) / 2) % 4
tpow0 = (weight_1[0] - m) % 8
tpow1 = (weight_1[1] - m) % 8
else:
m = (weight_2[0] / 2) % 4
tpow0 = (weight_1[0] + m) % 8
tpow1 = (weight_1[1] + m) % 8
if tpow0 > 0:
circuit.p((tpow0 * np.pi / 4), [0])
if xpow0 == 1:
circuit.x(0)
if tpow1 > 0:
circuit.p((tpow1 * np.pi / 4), [1])
if xpow1 == 1:
circuit.x(1)
circuit.cx(0, 1)
circuit.cx(1, 0)
if m > 0:
circuit.p((m * np.pi / 4), [1])
circuit.cx(0, 1)
return circuit