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. 23 from squander
import N_Qubit_State_Preparation_adaptive
26 from qiskit
import QuantumCircuit
37 qc = QuantumCircuit.from_qasm_file(
'../../benchmarks/IBM/alu-v4_37.qasm')
40 from qiskit
import execute
41 from qiskit
import Aer
42 import numpy.linalg
as LA
46 backend = Aer.get_backend(
'unitary_simulator')
49 job = execute(qc, backend)
54 Umtx = result.get_unitary(qc)
55 Umtx = np.asarray(Umtx)
59 State = np.ascontiguousarray( Umtx[:,0].copy() )
68 config = {
'agent_lifetime':1000,
70 'max_inner_iterations_agent': 100000,
71 'max_inner_iterations_compression': 1000,
72 'max_inner_iterations' : 100,
73 'max_inner_iterations_final': 100,
74 'Randomized_Radius': 0.3,
75 'randomized_adaptive_layers': 1,
76 'optimization_tolerance_agent': 1e-8,
77 'optimization_tolerance': 1e-16,
78 'convergence_length': 10}
84 cStatePrep = N_Qubit_State_Preparation_adaptive( State, config=config )
90 cStatePrep.set_Verbose( 3 )
96 for idx
in range(levels):
97 cStatePrep.add_Adaptive_Layers()
99 cStatePrep.add_Finalyzing_Layer_To_Gate_Structure()
103 parameter_num = cStatePrep.get_Parameter_Num()
105 parameters = np.random.randn( parameter_num )
106 cStatePrep.set_Optimized_Parameters( parameters )
112 for new_layer
in range(4):
116 print(
' Adding new layer to the gate structure')
125 cStatePrep.set_Optimizer(
"AGENTS")
128 cStatePrep.get_Initial_Circuit()
132 params_AGENTS = cStatePrep.get_Optimized_Parameters()
135 cStatePrep.set_Optimizer(
"BFGS")
138 cStatePrep.get_Initial_Circuit()
140 params_BFGS = cStatePrep.get_Optimized_Parameters()
141 decomp_error = cStatePrep.Optimization_Problem( params_BFGS )
143 if decomp_error <= config[
'optimization_tolerance_agent']:
147 cStatePrep.set_Optimized_Parameters( params_AGENTS )
150 cStatePrep.add_Layer_To_Imported_Gate_Structure()
163 cStatePrep.Finalize_Circuit()
168 print(
'Constructing quantum circuit:')
171 quantum_circuit = cStatePrep.get_Qiskit_Circuit()
173 print( quantum_circuit )