Quantum Gate Decomposer  v1.3
Powerful decomposition of almost any unitary into U3 and CNOT 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 (C) 2020 Peter Rakyta, Ph.D.
5 
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
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/.
18 
19 @author: Peter Rakyta, Ph.D.
20 """
21 
23 
24 
25 
26 
27 from qgd_python.N_Qubit_Decomposition import N_Qubit_Decomposition
28 
29 
30 print('******************** Decomposing general 3-qubit matrix *******************************')
31 
32 # cerate unitary q-bit matrix
33 from scipy.stats import unitary_group
34 import numpy as np
35 
36 
37 # the number of qubits spanning the unitary
38 qbit_num = 3
39 
40 # determine the soze of the unitary to be decomposed
41 matrix_size = int(2**qbit_num)
42 
43 # creating a random unitary to be decomposed
44 Umtx = unitary_group.rvs(matrix_size)
45 
46 # creating a class to decompose the
47 cDecompose = N_Qubit_Decomposition( Umtx.conj().T )
48 
49 # starting the decomposition
50 cDecompose.start_decomposition()
51 
52 # list the decomposing operations
53 cDecompose.list_operations()
54 
55 print(' ')
56 print(' ')
57 print(' ')
58 print('**********************************************************************************')
59 print('**********************************************************************************')
60 print('******************** Solving the 4th IBM chellenge *******************************')
61 print(' ')
62 print(' ')
63 print(' ')
64 
65 
66 #******************************
67 
68 from scipy.io import loadmat
69 
70 
71 data = loadmat('Umtx.mat')
72 
73 Umtx = data['Umtx']
74 
75 
76 
78 cDecompose = N_Qubit_Decomposition( Umtx.conj().T, optimize_layer_num=True, initial_guess="zeros" )
79 
80 
81 
83 cDecompose.set_identical_blocks( {4: 2, 3: 1} )
84 
85 # set the maximal number of layers in the decomposition
86 cDecompose.set_max_layer_num( {4: 9, 3:4})
87 
88 # set the number of iteration loops in the decomposition
89 cDecompose.set_iteration_loops({4: 3, 3: 3, 2: 3})
90 
91 # setting the verbosity of the decomposition
92 cDecompose.set_verbose( True )
93 
94 
95 
97 cDecompose.start_decomposition()
98 
99 # list the decomposing operations
100 cDecompose.list_operations()
101 
102 
103 
104 
105 print(' ')
106 print('Constructing quantum circuit:')
107 print(' ')
108 
109 quantum_circuit = cDecompose.get_quantum_circuit()
110 
111 print(quantum_circuit)
112 
113 from qiskit import execute
114 from qiskit import Aer
115 import numpy.linalg as LA
116 
117 # test the decomposition of the matrix
118 
119 backend = Aer.get_backend('unitary_simulator')
120 
121 
122 job = execute(quantum_circuit, backend)
123 
124 result = job.result()
125 
126 
127 decomposed_matrix = result.get_unitary(quantum_circuit)
128 
129 
130 
131 product_matrix = np.dot(Umtx, decomposed_matrix.conj().T)
132 
133 decomposition_error = LA.norm(product_matrix - np.identity(16)*product_matrix[0,0], 2)
134 
135 print('The error of the decomposition is ' + str(decomposition_error))
136 
137 
138 
A QGD Python interface class for the decomposition of N-qubit unitaries into U3 and CNOT gates.