Sequential Quantum Gate Decomposer  v1.9.6
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  // create empty configuration map
100  std::map<std::string, Config_Element> config;
101 
102  // creating the class for the decomposition. Here Umtx_adj is the complex transposition of unitary Umtx
103  N_Qubit_Decomposition cDecomposition(
104  Umtx_adj,
105  qbit_num,
106  /* optimize_layer_num= */ false,
107  config,
108  /* initial_guess= */ RANDOM
109  );
111 
113  // setting the number of successive identical layers used in the decomposition
114  std::map<int,int> identical_blocks;
115  identical_blocks[3] = 1;
116  identical_blocks[4] = 2;
117  cDecomposition.set_identical_blocks( identical_blocks );
118 
119  // setting the maximal number of layers used in the decomposition
120  std::map<int,int> num_of_layers;
121  num_of_layers[2] = 3;
122  num_of_layers[3] = 12;
123  num_of_layers[4] = 60;
124  num_of_layers[5] = 240;
125  num_of_layers[6] = 960;
126  num_of_layers[7] = 3775;
127  cDecomposition.set_max_layer_num( num_of_layers );
128 
129  // setting the number of optimization iteration loops in each step of the decomposition
130  std::map<int,int> num_of_iterations;
131  num_of_iterations[2] = 3;
132  num_of_iterations[3] = 1;
133  num_of_iterations[4] = 1;
134  cDecomposition.set_iteration_loops( num_of_iterations );
135 
136  // setting operation layer
137  cDecomposition.set_optimization_blocks( 20 );
138 
139  // setting the verbosity of the decomposition
140  cDecomposition.set_verbose( 3 );
142 
143  // setting the verbosity of the decomposition
144  verbose_level=1;
145  sstream << "Starting the decompsition" << std::endl;
146  output.print(sstream,verbose_level);
147 
148 
150  // starting the decomposition
151  cDecomposition.start_decomposition(/* prepare_export= */ true);
152 
153  cDecomposition.list_gates(1);
155 
156 
157 
158 
159  return 0;
160 
161 };
162 
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:136
Double-precision complex matrix (float64).
Definition: matrix.h:38
dictionary config
A class to create general random unitary matrices 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()
Construct a random unitary with internally generated float64 parameters.
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