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_adaptive
28 from qiskit
import QuantumCircuit
38 qc = QuantumCircuit.from_qasm_file(
'../../benchmarks/IBM/alu-v4_37.qasm')
41 from qiskit
import execute
42 from qiskit
import Aer
43 import numpy.linalg
as LA
47 backend = Aer.get_backend(
'unitary_simulator')
50 job = execute(qc, backend)
55 Umtx = result.get_unitary(qc)
56 Umtx = np.asarray(Umtx)
57 Umtx = Umtx*np.exp(-1j*(qc.global_phase))
66 config = {
'agent_lifetime':200,
67 'max_inner_iterations_agent': 100000,
68 'max_inner_iterations_compression': 100000,
69 'max_inner_iterations' : 10000,
70 'max_inner_iterations_final': 10000,
71 'Randomized_Radius': 0.3,
72 'randomized_adaptive_layers': 1,
73 'optimization_tolerance_agent': 1e-3,
74 'optimization_tolerance_': 1e-8}
81 cDecompose = N_Qubit_Decomposition_adaptive( Umtx.conj().T, config=config )
88 cDecompose.set_Verbose( 3 )
96 for idx
in range(levels):
97 cDecompose.add_Adaptive_Layers()
99 cDecompose.add_Finalyzing_Layer_To_Gate_Structure()
106 parameter_num = cDecompose.get_Parameter_Num()
107 parameters = np.zeros( (parameter_num,1), dtype=np.float64 )
108 cDecompose.set_Optimized_Parameters( parameters )
116 cDecompose.set_Optimizer(
"AGENTS")
124 cDecompose.get_Initial_Circuit()
132 cDecompose.set_Optimizer(
"BFGS")
137 cDecompose.get_Initial_Circuit()
144 cDecompose.Compress_Circuit()
151 cDecompose.Finalize_Circuit()
158 print(
'Constructing quantum circuit:')
161 quantum_circuit = cDecompose.get_Qiskit_Circuit()
163 print(quantum_circuit)
165 import numpy.linalg
as LA
168 from squander
import utils
169 decomposed_matrix = utils.get_unitary_from_qiskit_circuit( quantum_circuit )
170 product_matrix = np.dot(Umtx,decomposed_matrix.conj().T)
171 phase = np.angle(product_matrix[0,0])
172 product_matrix = product_matrix*np.exp(-1j*phase)
174 product_matrix = np.eye(product_matrix.shape[0])*2 - product_matrix - product_matrix.conj().T
176 decomposition_error = (np.real(np.trace(product_matrix)))/2
178 print(
'The error of the decomposition is ' + str(decomposition_error))