Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
example_bfgs2.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 @author: Peter Rakyta, Ph.D.
19 """
20 
22 
23 
24 from squander import N_Qubit_Decomposition
25 
27 from squander import N_Qubit_Decomposition_adaptive
28 
29 
30 
31 print(' ')
32 print(' ')
33 print(' ')
34 print('**********************************************************************************')
35 print('**********************************************************************************')
36 print('******************** Solving the 4th IBM chellenge *******************************')
37 print(' ')
38 print(' ')
39 print(' ')
40 
41 
42 #******************************
43 
44 from scipy.io import loadmat
45 import numpy as np
46 
47 
48 from squander import utils
49 
50 
51 data = loadmat('examples/decomposition/Umtx.mat')
52 
53 Umtx = data['Umtx']
54 
55 
56 
57 # determine the size of the unitary to be decomposed
58 matrix_size = len(Umtx)
59 
60 
62 method = "basin_hopping"
63 #method = "differential_evolution"
64 #method = "dual_annealing"
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,
72  "de_strategy": 0, #0 is STRAT_BEST1BIN, 1 is STRAT_RAND1BIN
73  "de_popsize": 15, "de_mutation": 0.8, "de_recombination": 0.7,
74  "de_tol": 1e-6, "de_init": 0, #0 is INIT_RANDOM, 1 is INIT_LHS
75  "de_polish": 1
76  }
77 elif method == "dual_annealing": #default
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
81  }
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)
85 
86 
87 # set the optimization engine to be used
88 
89 cDecompose.set_Optimizer("BFGS2")
90 
91 
92 
94 cDecompose.Start_Decomposition()
95 
96 # list the decomposing operations
97 cDecompose.List_Gates()
98 
99 
100 
101 
102 print(' ')
103 print('Constructing quantum circuit:')
104 print(' ')
105 
106 quantum_circuit = cDecompose.get_Qiskit_Circuit()
107 
108 print(quantum_circuit)
109 
110 import numpy.linalg as LA
111 
112 
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)
117 
118 product_matrix = np.eye(matrix_size)*2 - product_matrix - product_matrix.conj().T
119 # the error of the decomposition
120 decomposition_error = (np.real(np.trace(product_matrix)))/2
121 
122 print('The error of the decomposition is ' + str(decomposition_error))
123 
124 
125 
126 
127 
128 
129 
130 
131 
132