Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
example.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('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 cDecompose = N_Qubit_Decomposition_adaptive( Umtx.conj().T, level_limit_max=5, level_limit_min=1 )
63 
64 
65 # set the optimization engine to be used (default is BFGS)
66 cDecompose.set_Optimizer("BFGS")
67 
68 
69 
71 cDecompose.Start_Decomposition()
72 
73 # list the decomposing operations
74 cDecompose.List_Gates()
75 
76 
77 
78 
79 print(' ')
80 print('Constructing quantum circuit:')
81 print(' ')
82 
83 quantum_circuit = cDecompose.get_Qiskit_Circuit()
84 
85 print(quantum_circuit)
86 
87 import numpy.linalg as LA
88 
89 
90 decomposed_matrix = utils.get_unitary_from_qiskit_circuit( quantum_circuit )
91 product_matrix = np.dot(Umtx,decomposed_matrix.conj().T)
92 phase = np.angle(product_matrix[0,0])
93 product_matrix = product_matrix*np.exp(-1j*phase)
94 
95 product_matrix = np.eye(matrix_size)*2 - product_matrix - product_matrix.conj().T
96 # the error of the decomposition
97 decomposition_error = (np.real(np.trace(product_matrix)))/2
98 
99 print('The error of the decomposition is ' + str(decomposition_error))
100 
101 
102 
103 
104 
105 
106 
107 
108 
109