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 @author: Peter Rakyta, Ph.D. 24 from squander
import N_Qubit_Decomposition
27 from squander
import N_Qubit_Decomposition_adaptive
34 print(
'**********************************************************************************')
35 print(
'**********************************************************************************')
36 print(
'******************** Solving the 4th IBM chellenge *******************************')
44 from scipy.io
import loadmat
48 from squander
import utils
51 data = loadmat(
'examples/decomposition/Umtx.mat')
58 matrix_size = len(Umtx)
62 method =
"basin_hopping" 65 iteration_loops = {4: 3, 3: 3, 2: 3}
66 if method ==
"basin_hopping":
67 config = {
"use_basin_hopping": 1,
68 "bh_T": 1.0,
"bh_stepsize": 0.5,
"bh_interval": 50,
69 "bh_target_accept_rate": 0.5,
"bh_stepwise_factor": 0.9}
70 elif method ==
"differential_evolution":
71 config = {
"use_differential_evolution": 1,
73 "de_popsize": 15,
"de_mutation": 0.8,
"de_recombination": 0.7,
74 "de_tol": 1e-6,
"de_init": 0,
77 elif method ==
"dual_annealing":
78 config = {
"use_dual_annealing": 1,
79 "da_initial_temp": 5330.0,
"da_restart_temp_ratio": 2e-5,
"da_visit": 2.62,
"da_accept": -5.0,
80 "da_maxiter": max(iteration_loops.values()),
"da_maxfun": 10000000,
"da_tol": 1e-6,
"da_no_local_search": 0
82 print(
"Using config:", config)
83 cDecompose = N_Qubit_Decomposition_adaptive( Umtx.conj().T, config=config, level_limit_max=5, level_limit_min=1 )
84 cDecompose.set_Iteration_Loops(iteration_loops)
89 cDecompose.set_Optimizer(
"BFGS2")
94 cDecompose.Start_Decomposition()
97 cDecompose.List_Gates()
103 print(
'Constructing quantum circuit:')
106 quantum_circuit = cDecompose.get_Qiskit_Circuit()
108 print(quantum_circuit)
110 import numpy.linalg
as LA
113 decomposed_matrix = utils.get_unitary_from_qiskit_circuit( quantum_circuit )
114 product_matrix = np.dot(Umtx,decomposed_matrix.conj().T)
115 phase = np.angle(product_matrix[0,0])
116 product_matrix = product_matrix*np.exp(-1j*phase)
118 product_matrix = np.eye(matrix_size)*2 - product_matrix - product_matrix.conj().T
120 decomposition_error = (np.real(np.trace(product_matrix)))/2
122 print(
'The error of the decomposition is ' + str(decomposition_error))