3 Created on Fri Jun 26 14:42:56 2020 4 Copyright 2020 Peter Rakyta, Ph.D. 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 10 http://www.apache.org/licenses/LICENSE-2.0 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. 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/. 21 @author: Peter Rakyta, Ph.D. 29 from scipy.stats
import unitary_group
31 from squander
import utils
35 from mpi4py
import MPI
37 except ModuleNotFoundError:
44 """This is a test class of the python iterface to the decompsition classes of the QGD package""" 48 This method is called by pytest. 49 Test to decompose a 4-qubit unitary of the IBM chellenge 53 from squander
import N_Qubit_Decomposition_adaptive
54 from scipy.io
import loadmat
57 data = loadmat(
'data/Umtx.mat')
62 config = {
'max_outer_iterations': 10,
63 'max_inner_iterations': 300000,
64 'max_inner_iterations_compression': 10000,
65 'max_inner_iterations_final': 1000,
66 'randomized_adaptive_layers': 1,
67 'export_circuit_2_binary': 1,
68 'optimization_tolerance': 1e-8 }
78 cDecompose.set_Verbose( 3 )
82 for idx
in range(levels):
83 cDecompose.add_Adaptive_Layers()
85 cDecompose.add_Finalyzing_Layer_To_Gate_Structure()
88 rng = np.random.default_rng( 42 )
89 num_of_parameters = cDecompose.get_Parameter_Num()
90 parameters = rng.random(num_of_parameters)*2*np.pi
92 cDecompose.set_Optimized_Parameters( parameters )
96 cDecompose.get_Initial_Circuit()
99 cDecompose.Compress_Circuit()
102 cDecompose.Finalize_Circuit()
105 cDecompose.List_Gates()
108 quantum_circuit = cDecompose.get_Qiskit_Circuit()
111 print(quantum_circuit)
113 import numpy.linalg
as LA
116 decomposed_matrix = utils.get_unitary_from_qiskit_circuit( quantum_circuit )
117 product_matrix = np.dot(Umtx,decomposed_matrix.conj().T)
118 phase = np.angle(product_matrix[0,0])
119 product_matrix = product_matrix*np.exp(-1j*phase)
121 product_matrix = np.eye(16)*2 - product_matrix - product_matrix.conj().T
123 decomposition_error = (np.real(np.trace(product_matrix)))/2
125 print(
'The error of the decomposition is ' + str(decomposition_error))
127 assert( decomposition_error < 1e-3 )
132 This method is called by pytest. 133 Test to decompose a 4-qubit unitary of the IBM chellenge 137 from squander
import N_Qubit_Decomposition_adaptive
138 from scipy.io
import loadmat
141 data = loadmat(
'data/Umtx.mat')
146 config = {
'max_outer_iterations': 10,
147 'max_inner_iterations': 300000,
148 'max_inner_iterations_compression': 10000,
149 'max_inner_iterations_final': 1000,
150 'randomized_adaptive_layers': 1,
151 'export_circuit_2_binary': 1,
152 'optimization_tolerance': 1e-8 }
160 cDecompose.set_Verbose( 3 )
165 for idx
in range(levels):
166 cDecompose.add_Adaptive_Layers()
168 cDecompose.add_Finalyzing_Layer_To_Gate_Structure()
172 rng = np.random.default_rng( 42 )
173 num_of_parameters = cDecompose.get_Parameter_Num()
174 parameters = rng.random(num_of_parameters)*2*np.pi
176 cDecompose.set_Optimized_Parameters( parameters )
179 cDecompose.get_Initial_Circuit()
182 cDecompose.Finalize_Circuit()
185 cDecompose.List_Gates()
188 quantum_circuit = cDecompose.get_Qiskit_Circuit()
191 print(quantum_circuit)
193 import numpy.linalg
as LA
196 decomposed_matrix = utils.get_unitary_from_qiskit_circuit( quantum_circuit )
197 product_matrix = np.dot(Umtx,decomposed_matrix.conj().T)
198 phase = np.angle(product_matrix[0,0])
199 product_matrix = product_matrix*np.exp(-1j*phase)
201 product_matrix = np.eye(16)*2 - product_matrix - product_matrix.conj().T
203 decomposition_error = (np.real(np.trace(product_matrix)))/2
205 print(
'The error of the decomposition is ' + str(decomposition_error))
207 assert( decomposition_error < 1e-3 )
212 This method is called by pytest. 213 Test to decompose a 4-qubit unitary of the IBM chellenge 217 from squander
import N_Qubit_Decomposition_adaptive
218 from scipy.io
import loadmat
221 data = loadmat(
'data/Umtx.mat')
227 config = {
'max_outer_iterations': 10,
228 'max_inner_iterations': 300000,
229 'max_inner_iterations_compression': 10000,
230 'max_inner_iterations_final': 1000,
231 'randomized_adaptive_layers': 1,
232 'export_circuit_2_binary': 1,
233 'optimization_tolerance': 1e-8 }
241 cDecompose.set_Verbose( 3 )
246 cDecompose.set_Gate_Structure_From_Binary(
"circuit_squander.binary")
249 cDecompose.Compress_Circuit()
252 cDecompose.Finalize_Circuit()
255 cDecompose.List_Gates()
258 quantum_circuit = cDecompose.get_Qiskit_Circuit()
261 print(quantum_circuit)
263 import numpy.linalg
as LA
266 decomposed_matrix = utils.get_unitary_from_qiskit_circuit( quantum_circuit )
267 product_matrix = np.dot(Umtx,decomposed_matrix.conj().T)
268 phase = np.angle(product_matrix[0,0])
269 product_matrix = product_matrix*np.exp(-1j*phase)
271 product_matrix = np.eye(16)*2 - product_matrix - product_matrix.conj().T
273 decomposition_error = (np.real(np.trace(product_matrix)))/2
275 print(
'The error of the decomposition is ' + str(decomposition_error))
277 assert( decomposition_error < 1e-3 )
282 This method is called by pytest. 283 Test to decompose a 4-qubit unitary of the IBM chellenge 287 from squander
import N_Qubit_Decomposition_adaptive
288 from scipy.io
import loadmat
291 data = loadmat(
'data/Umtx.mat')
294 config = {
'max_outer_iterations': 1,
295 'max_inner_iterations_agent': 25000,
296 'max_inner_iterations_compression': 10000,
297 'max_inner_iterations' : 500,
298 'max_inner_iterations_final': 5000,
299 'Randomized_Radius': 0.3,
300 'randomized_adaptive_layers': 1,
301 'optimization_tolerance_agent': 1e-4,
302 'optimization_tolerance': 1e-5,
310 cDecompose.set_Verbose( 3 )
315 for idx
in range(levels):
316 cDecompose.add_Adaptive_Layers()
318 cDecompose.add_Finalyzing_Layer_To_Gate_Structure()
323 rng = np.random.default_rng( 42 )
324 num_of_parameters = cDecompose.get_Parameter_Num()
325 parameters = rng.random(num_of_parameters)*2*np.pi
327 cDecompose.set_Optimized_Parameters( parameters )
330 cDecompose.set_Optimizer(
"AGENTS")
333 cDecompose.get_Initial_Circuit()
336 cDecompose.set_Optimizer(
"BFGS")
339 cDecompose.get_Initial_Circuit()
342 cDecompose.Compress_Circuit()
345 cDecompose.Finalize_Circuit()
348 cDecompose.List_Gates()
351 quantum_circuit = cDecompose.get_Qiskit_Circuit()
354 print(quantum_circuit)
356 import numpy.linalg
as LA
359 decomposed_matrix = utils.get_unitary_from_qiskit_circuit( quantum_circuit )
360 product_matrix = np.dot(Umtx,decomposed_matrix.conj().T)
361 phase = np.angle(product_matrix[0,0])
362 product_matrix = product_matrix*np.exp(-1j*phase)
364 product_matrix = np.eye(16)*2 - product_matrix - product_matrix.conj().T
366 decomposition_error = (np.real(np.trace(product_matrix)))/2
368 print(
'The error of the decomposition is ' + str(decomposition_error))
370 assert( decomposition_error < 1e-3 )
def test_IBM_Chellenge_compression_only(self)
def test_IBM_Chellenge_multiple_optim(self)
A base class to determine the decomposition of an N-qubit unitary into a sequence of CNOT and U3 gate...
def test_IBM_Chellenge_no_compression(self)
def test_IBM_Chellenge_full(self)