Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
test_IBM.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 """
3 Created on Fri Jun 26 14:42:56 2020
4 Copyright 2020 Peter Rakyta, Ph.D.
5 
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see http://www.gnu.org/licenses/.
20 
21 @author: Peter Rakyta, Ph.D.
22 """
23 
25 
26 
27 
28 # cerate unitary q-bit matrix
29 from scipy.stats import unitary_group
30 import numpy as np
31 from squander import utils
32 
33 
34 try:
35  from mpi4py import MPI
36  MPI_imported = True
37 except ModuleNotFoundError:
38  MPI_imported = False
39 
40 
41 
43  """This is a test class of the python iterface to the decompsition classes of the QGD package"""
44 
45 
46 
47  def test_IBM_Chellenge(self):
48  r"""
49  This method is called by pytest.
50  Test to decompose a 4-qubit unitary of the IBM chellenge
51 
52  """
53 
54  from squander import N_Qubit_Decomposition
55  from scipy.io import loadmat
56 
57  # load the unitary from file
58  data = loadmat('data/Umtx.mat')
59  # The unitary to be decomposed
60  Umtx = data['Umtx']
61 
62 
63  # creating a class to decompose the unitary
64  cDecompose = N_Qubit_Decomposition( Umtx.conj().T, optimize_layer_num=True, initial_guess="CLOSE_TO_ZERO" )
65 
66 
67  # set the number of successive identical blocks in the optimalization of disentanglement of the n-th qubits
68  cDecompose.set_Identical_Blocks( {4: 2, 3: 1} )
69 
70  # set the maximal number of layers in the decomposition
71  cDecompose.set_Max_Layer_Num( {4: 9, 3:4} )
72 
73  # set the number of iteration loops in the decomposition
74  cDecompose.set_Iteration_Loops({4: 3, 3: 3, 2: 3})
75 
76  # setting the verbosity of the decomposition
77  #cDecompose.set_Verbose( True )
78 
79  # set the number of block to be optimized in one shot
80  cDecompose.set_Optimization_Blocks( 20 )
81 
82  # starting the decomposition
83  cDecompose.Start_Decomposition()
84 
85  # list the decomposing operations
86  cDecompose.List_Gates()
87 
88  # get the decomposing operations
89  quantum_circuit = cDecompose.get_Qiskit_Circuit()
90 
91  # print the quantum circuit
92  print(quantum_circuit)
93 
94 
95  import numpy.linalg as LA
96 
97  # test the decomposition of the matrix
98  # the unitary matrix from the result object
99  decomposed_matrix = utils.get_unitary_from_qiskit_circuit( quantum_circuit )
100  product_matrix = np.dot(Umtx,decomposed_matrix.conj().T)
101  phase = np.angle(product_matrix[0,0])
102  product_matrix = product_matrix*np.exp(-1j*phase)
103 
104  product_matrix = np.eye(16)*2 - product_matrix - product_matrix.conj().T
105  # the error of the decomposition
106  decomposition_error = (np.real(np.trace(product_matrix)))/2
107 
108  print('The error of the decomposition is ' + str(decomposition_error))
109 
110  assert( decomposition_error < 1e-3 )
111 
112 
113 
115  r"""
116  This method is called by pytest.
117  Test to decompose a 4-qubit unitary of the IBM chellenge
118 
119  """
120 
121  from squander import N_Qubit_Decomposition_adaptive
122  from scipy.io import loadmat
123 
124  # load the unitary from file
125  data = loadmat('data/Umtx.mat')
126  # The unitary to be decomposed
127  Umtx = data['Umtx']
128 
129 
130  # creating a class to decompose the unitary
131  cDecompose = N_Qubit_Decomposition_adaptive( Umtx.conj().T, level_limit_max=5, level_limit_min=0 )
132 
133 
134  # setting the verbosity of the decomposition
135  cDecompose.set_Verbose( 3 )
136 
137  # starting the decomposition
138  cDecompose.Start_Decomposition()
139 
140  # list the decomposing operations
141  cDecompose.List_Gates()
142 
143  # get the decomposing operations
144  quantum_circuit = cDecompose.get_Qiskit_Circuit()
145 
146  # print the quantum circuit
147  print(quantum_circuit)
148 
149  import numpy.linalg as LA
150 
151  # the unitary matrix from the result object
152  decomposed_matrix = utils.get_unitary_from_qiskit_circuit( quantum_circuit )
153  product_matrix = np.dot(Umtx,decomposed_matrix.conj().T)
154  phase = np.angle(product_matrix[0,0])
155  product_matrix = product_matrix*np.exp(-1j*phase)
156 
157  product_matrix = np.eye(16)*2 - product_matrix - product_matrix.conj().T
158  # the error of the decomposition
159  decomposition_error = (np.real(np.trace(product_matrix)))/2
160 
161  print('The error of the decomposition is ' + str(decomposition_error))
162 
163  assert( decomposition_error < 1e-3 )
164 
165 
166 
167 
168 
170  r"""
171  This method is called by pytest.
172  Test to decompose a 4-qubit unitary of the IBM chellenge
173 
174  """
175 
176  from squander import N_Qubit_Decomposition_adaptive
177  from scipy.io import loadmat
178 
179  # load the unitary from file
180  data = loadmat('data/Umtx.mat')
181  # The unitary to be decomposed
182  Umtx = data['Umtx']
183  Umtx = Umtx[0:14,:]
184 
185 
186  # creating a class to decompose the unitary
187  cDecompose = N_Qubit_Decomposition_adaptive( Umtx.conj().T, level_limit_max=5, level_limit_min=0 )
188 
189 
190  # setting the verbosity of the decomposition
191  cDecompose.set_Verbose( 3 )
192 
193  # starting the decomposition
194  cDecompose.Start_Decomposition()
195 
196  # list the decomposing operations
197  cDecompose.List_Gates()
198 
199  # get the decomposing operations
200  quantum_circuit = cDecompose.get_Qiskit_Circuit()
201 
202  # print the quantum circuit
203  print(quantum_circuit)
204 
205  import numpy.linalg as LA
206 
207  # the unitary matrix from the result object
208  decomposed_matrix = utils.get_unitary_from_qiskit_circuit( quantum_circuit )
209  decomposed_matrix = decomposed_matrix[0:14,:]
210  product_matrix = np.dot(Umtx,decomposed_matrix.conj().T)
211  phase = np.angle(product_matrix[0,0])
212  product_matrix = product_matrix*np.exp(-1j*phase)
213 
214  print( product_matrix.shape )
215 
216  product_matrix = np.eye(product_matrix.shape[0])*2 - product_matrix - product_matrix.conj().T
217  # the error of the decomposition
218  decomposition_error = (np.real(np.trace(product_matrix)))/2
219 
220  print('The error of the decomposition is ' + str(decomposition_error))
221 
222  assert( decomposition_error < 1e-3 )
223 
224 
225 
227  r"""
228  This method is called by pytest.
229  Test to decompose a 4-qubit unitary of the IBM chellenge
230 
231  """
232 
233  from squander import N_Qubit_Decomposition_Tree_Search
234  from scipy.io import loadmat
235 
236  # load the unitary from file
237  data = loadmat('data/Umtx.mat')
238  # The unitary to be decomposed
239  Umtx = data['Umtx']
240 
241 
242  # creating a class to decompose the unitary
243  cDecompose = N_Qubit_Decomposition_Tree_Search( Umtx.conj().T )
244 
245 
246  # setting the verbosity of the decomposition
247  cDecompose.set_Verbose( 3 )
248 
249  # starting the decomposition
250  cDecompose.Start_Decomposition()
251 
252  # list the decomposing operations
253  cDecompose.List_Gates()
254 
255  # get the decomposing operations
256  quantum_circuit = cDecompose.get_Qiskit_Circuit()
257 
258  # print the quantum circuit
259  print(quantum_circuit)
260 
261  import numpy.linalg as LA
262 
263  # the unitary matrix from the result object
264  decomposed_matrix = utils.get_unitary_from_qiskit_circuit( quantum_circuit )
265  product_matrix = np.dot(Umtx,decomposed_matrix.conj().T)
266  phase = np.angle(product_matrix[0,0])
267  product_matrix = product_matrix*np.exp(-1j*phase)
268 
269  product_matrix = np.eye(16)*2 - product_matrix - product_matrix.conj().T
270  # the error of the decomposition
271  decomposition_error = (np.real(np.trace(product_matrix)))/2
272 
273  print('The error of the decomposition is ' + str(decomposition_error))
274 
275  assert( decomposition_error < 1e-3 )
276 
277 
278 
279 
281  r"""
282  This method is called by pytest.
283  Test to decompose a 4-qubit unitary of the IBM chellenge
284 
285  """
286 
287  from squander import N_Qubit_Decomposition_Tabu_Search
288  from scipy.io import loadmat
289 
290  # load the unitary from file
291  data = loadmat('data/Umtx.mat')
292  # The unitary to be decomposed
293  Umtx = data['Umtx']
294 
295 
296  # creating a class to decompose the unitary
297  cDecompose = N_Qubit_Decomposition_Tabu_Search( Umtx.conj().T )
298 
299 
300  # setting the verbosity of the decomposition
301  cDecompose.set_Verbose( 3 )
302 
303  # starting the decomposition
304  cDecompose.Start_Decomposition()
305 
306  # list the decomposing operations
307  cDecompose.List_Gates()
308 
309  # get the decomposing operations
310  quantum_circuit = cDecompose.get_Qiskit_Circuit()
311 
312  # print the quantum circuit
313  print(quantum_circuit)
314 
315  import numpy.linalg as LA
316 
317  # the unitary matrix from the result object
318  decomposed_matrix = utils.get_unitary_from_qiskit_circuit( quantum_circuit )
319  product_matrix = np.dot(Umtx,decomposed_matrix.conj().T)
320  phase = np.angle(product_matrix[0,0])
321  product_matrix = product_matrix*np.exp(-1j*phase)
322 
323  product_matrix = np.eye(16)*2 - product_matrix - product_matrix.conj().T
324  # the error of the decomposition
325  decomposition_error = (np.real(np.trace(product_matrix)))/2
326 
327  print('The error of the decomposition is ' + str(decomposition_error))
328 
329  assert( decomposition_error < 1e-3 )
330 
A base class to determine the decomposition of an N-qubit unitary into a sequence of CNOT and U3 gate...
A base class to determine the decomposition of an N-qubit unitary into a sequence of CNOT and U3 gate...
A base class to determine the decomposition of an N-qubit unitary into a sequence of CNOT and U3 gate...
A base class to determine the decomposition of an N-qubit unitary into a sequence of CNOT and U3 gate...