Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
decomposition_test.cpp
Go to the documentation of this file.
1 /*
2 Created on Fri Jun 26 14:14:12 2020
3 Copyright 2020 Peter Rakyta, Ph.D.
4 
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8 
9  http://www.apache.org/licenses/LICENSE-2.0
10 
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see http://www.gnu.org/licenses/.
19 
20 @author: Peter Rakyta, Ph.D.
21 */
26 #include <iostream>
27 #include <stdio.h>
28 #include <map>
29 
30 
32 #include "common.h"
33 #include "N_Qubit_Decomposition.h"
34 #include "Random_Unitary.h"
35 #include "logging.h"
37 
38 using namespace std;
39 
40 
41 
45 int main() {
46 
47 
49  std::stringstream sstream;
50 
53 
55  int verbose_level;
56 
57  //Setting the verbosity level of the Test of N qubit decomposition
58  verbose_level=1;
59  sstream << std::endl << std::endl << "****************************************" << std::endl;
60  sstream << "Test of N qubit decomposition" << std::endl;
61  sstream << "****************************************" << std::endl << std::endl << std::endl;
62  output.print(sstream,verbose_level);
63 
65  // The number of qubits spanning the random unitary
66  int qbit_num = 4;
67 
68  // the number of rows of the random unitary
69  int matrix_size = Power_of_2(qbit_num);
70 
71  // creating random unitary constructing from 6 CNOT gates.
72  int cnot_num = 6;
73  Matrix Umtx_few_CNOT = few_CNOT_unitary( qbit_num, cnot_num);
75 
76 
78  // creating class to generate general random unitary
79  Random_Unitary ru = Random_Unitary(matrix_size);
80  // create general random unitary
83 
84 
85 
86  // construct the complex transpose of the random unitary
87  Matrix Umtx_adj = Matrix(matrix_size, matrix_size);
88  for (int element_idx=0; element_idx<matrix_size*matrix_size; element_idx++) {
89  // determine the row and column index of the element to be filled.
90  int col_idx = element_idx % matrix_size;
91  int row_idx = int((element_idx-col_idx)/matrix_size);
92 
93  // setting the complex conjugate of the element in the adjungate matrix
94  QGD_Complex16 element = Umtx[col_idx*matrix_size + row_idx];
95  Umtx_adj[element_idx].real = element.real;
96  Umtx_adj[element_idx].imag = -element.imag;
97  }
99  // creating the class for the decomposition. Here Umtx_adj is the complex transposition of unitary Umtx
100  N_Qubit_Decomposition cDecomposition =
101  N_Qubit_Decomposition( Umtx_adj, qbit_num, /* optimize_layer_num= */ false, /* initial_guess= */ RANDOM );
103 
105  // setting the number of successive identical layers used in the decomposition
106  std::map<int,int> identical_blocks;
107  identical_blocks[3] = 1;
108  identical_blocks[4] = 2;
109  cDecomposition.set_identical_blocks( identical_blocks );
110 
111  // setting the maximal number of layers used in the decomposition
112  std::map<int,int> num_of_layers;
113  num_of_layers[2] = 3;
114  num_of_layers[3] = 12;
115  num_of_layers[4] = 60;
116  num_of_layers[5] = 240;
117  num_of_layers[6] = 960;
118  num_of_layers[7] = 3775;
119  cDecomposition.set_max_layer_num( num_of_layers );
120 
121  // setting the number of optimization iteration loops in each step of the decomposition
122  std::map<int,int> num_of_iterations;
123  num_of_iterations[2] = 3;
124  num_of_iterations[3] = 1;
125  num_of_iterations[4] = 1;
126  cDecomposition.set_iteration_loops( num_of_iterations );
127 
128  // setting operation layer
129  cDecomposition.set_optimization_blocks( 20 );
130 
131  // setting the verbosity of the decomposition
132  cDecomposition.set_verbose( 3 );
134 
135  // setting the verbosity of the decomposition
136  verbose_level=1;
137  sstream << "Starting the decompsition" << std::endl;
138  output.print(sstream,verbose_level);
139 
140 
142  // starting the decomposition
143  cDecomposition.start_decomposition(/* prepare_export= */ true);
144 
145  cDecomposition.list_gates(1);
147 
148 
149 
150 
151  return 0;
152 
153 };
154 
int main()
Decomposition of general random unitary matrix into U3 and CNOT gates.
Matrix few_CNOT_unitary(int qbit_num, int cnot_num)
Call to create a random unitary constructed by CNOT operation between randomly chosen qubits and by r...
void print(const std::stringstream &sstream, int verbose_level=1) const
Call to print output messages in the function of the verbosity level.
Definition: logging.cpp:55
Header file for a class containing basic methods for setting up the verbosity level.
A class containing basic methods for setting up the verbosity level.
Definition: logging.h:43
matrix_size
[load Umtx]
Definition: example.py:58
Umtx
The unitary to be decomposed.
Definition: example.py:53
Structure type representing complex numbers in the SQUANDER package.
Definition: QGDTypes.h:38
int Power_of_2(int n)
Calculates the n-th power of 2.
Definition: common.cpp:117
Class to store data of complex arrays and its properties.
Definition: matrix.h:38
A class to cerate general random unitary matrix according to arXiv:1303:5904v1.
Header file for commonly used functions and wrappers to CBLAS functions.
double real
the real part of a complex number
Definition: QGDTypes.h:40
Matrix Construct_Unitary_Matrix()
Call to create a random unitary.
Header file for a class to determine the decomposition of an N-qubit unitary into a sequence of CNOT ...
double imag
the imaginary part of a complex number
Definition: QGDTypes.h:42