2 Copyright 2020 Peter Rakyta, Ph.D. 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 8 http://www.apache.org/licenses/LICENSE-2.0 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 16 You should have received a copy of the GNU General Public License 17 along with this program. If not, see http://www.gnu.org/licenses/. 22 import scipy.constants
as cnst
23 import scipy.linalg
as linalg
25 from squander
import N_Qubit_Decomposition
26 from squander
import N_Qubit_Decomposition_custom
28 from qiskit
import QuantumCircuit
29 from qiskit.visualization
import plot_histogram
30 from squander
import utils
33 from mpi4py
import MPI
35 except ModuleNotFoundError:
40 """This is a test class of the python iterface to the decompsition classes of the QGD package""" 45 Add layers to disentangle the 3rd qubit from the others 46 linear chain with IBM native operations 50 from squander
import Circuit
55 Circuit_ret = Circuit( qbit_num )
58 for idx
in range(0,layer_num,2):
61 Layer = Circuit( qbit_num )
84 Circuit_ret.add_Circuit( Layer )
88 Layer = Circuit( qbit_num )
109 Layer.add_CNOT( 0, 1)
111 Circuit_ret.add_Circuit( Layer )
121 Add layers to disentangle the final 2 qubits 122 linear chain with IBM native operations 126 from squander
import Circuit
131 if Circuit_ret ==
None:
133 Circuit_ret = Circuit( qbit_num )
136 for idx
in range(layer_num):
139 Layer = Circuit( qbit_num )
160 Layer.add_CNOT( 0, 1)
162 Circuit_ret.add_Circuit( Layer )
171 Rotating qubits into state |0> 175 from squander
import Circuit
179 if Circuit_ret ==
None:
181 Circuit_ret = Circuit( qbit_num )
184 for idx
in range(qbit_num):
187 Layer = Circuit( qbit_num )
198 Circuit_ret.add_Circuit( Layer )
210 U = np.hstack((U,np.zeros((7,1))))
211 U = np.vstack((U,np.zeros((1,8))))
224 matrix_size =
int(2**qbit_num)
229 [[240, -87.7, 5.5, -5.9, 6.7, -13.7, -9.9],
230 [-87.7, 315, 30.8, 8.2, 0.7, 11.8, 4.3],
231 [ 5.5, 30.8, 0, -53.5, -2.2, -9.6, 6.0],
232 [-5.9, 8.2, -53.5, 130, -70.7, -17.0, -63.3],
233 [ 6.7, 0.7, -2.2, -70.7, 285, 81.1, -1.3],
234 [-13.7, 11.8, -9.6, -17.0, 81.1, 435, 39.7],
235 [-9.9, 4.3, 6.0, -63.3, -1.3, 39.7, 245]])
248 config = {
'max_outer_iterations': 1,
249 'max_inner_iterations' : 500,
250 'optimization_tolerance': 1e-8,
257 decomp = N_Qubit_Decomposition_custom( Umtx.conj().T, initial_guess=
"zeros", config=config )
260 decomp.set_Gate_Structure( gate_structure )
263 rng = np.random.default_rng( 42 )
264 num_of_parameters = decomp.get_Parameter_Num()
265 parameters = rng.random(num_of_parameters)*2*np.pi
267 decomp.set_Optimized_Parameters( parameters )
271 decomp.set_Optimization_Tolerance( 1e-6 )
274 decomp.set_Optimization_Blocks( 50 )
276 decomp.set_Cost_Function_Variant( 3 )
278 decomp.set_Optimizer(
"BFGS")
280 decomp.set_Verbose(3)
283 decomp.Start_Decomposition()
293 quantum_circuit = decomp.get_Qiskit_Circuit()
298 decomposed_matrix = np.asarray( utils.get_unitary_from_qiskit_circuit( quantum_circuit ) )
299 product_matrix = np.dot(Umtx,decomposed_matrix.conj().T)
300 phase = np.angle(product_matrix[0,0])
301 product_matrix = product_matrix*np.exp(-1j*phase)
303 product_matrix = np.eye(matrix_size)*2 - product_matrix - product_matrix.conj().T
305 decomposition_error = (np.real(np.trace(product_matrix)))/2
307 print(
'The error of the decomposition is ' + str(decomposition_error))
309 assert( decomposition_error < 1e-3 )
def create_custom_gate_structure_lin_3qubit(self, layer_num=2)
def test_N_Qubit_Decomposition_3qubit(self)
def create_custom_gate_structure_lin_2qubit(self, layer_num=1, Circuit_ret=None)
def create_custom_gate_structure_finalyzing(self, qbit_num, Circuit_ret=None)