Quantum Gate Decomposer  v1.3
Powerful decomposition of almost any unitary into U3 and CNOT gates
python_interface.cpp
Go to the documentation of this file.
1 /*
2 Created on Fri Jun 26 14:13:26 2020
3 Copyright (C) 2020 Peter Rakyta, Ph.D.
4 
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see http://www.gnu.org/licenses/.
17 
18 @author: Peter Rakyta, Ph.D.
19 */
24 #include "qgd/python_interface.h"
26 #include "qgd/matrix.h"
27 
28 
29 extern "C" {
30 
31 
41 void* iface_new_N_Qubit_Decomposition( double* mtx_real, double* mtx_imag, int qbit_num, bool optimize_layer_num, int initial_guess_num ) {
42 
44 
45  // combining real and imaginary parts of the matrix inti MKL complex matrix
46  int element_num = matrix_size*matrix_size;
48 
49  //#pragma omp parallel for
50  for(int idx = 0; idx < element_num; idx++) {
51  Umtx[idx].real = mtx_real[idx];
52  Umtx[idx].imag = mtx_imag[idx];
53  }
54 
55  // setting the initial guess type
56  guess_type initial_guess;
57  if ( initial_guess_num==0 ) {
58  initial_guess = ZEROS;
59  }
60  else if ( initial_guess_num==1 ) {
61  initial_guess = RANDOM;
62  }
63  else if ( initial_guess_num==2 ) {
64  initial_guess = CLOSE_TO_ZERO;
65  }
66  else {
67  printf("Wrong initial guess\n");
68  exit(-1);
69  }
70 
71  // creating an instance of class N_Qubit_decomposition
72  N_Qubit_Decomposition* instance = new N_Qubit_Decomposition( Umtx, qbit_num, optimize_layer_num, initial_guess );
73 
74  return (void*)instance;
75 
76 
77 }
78 
79 
80 
85 int iface_start_decomposition( void* ptr ) {
86 
87  N_Qubit_Decomposition* instance = reinterpret_cast<N_Qubit_Decomposition*>(ptr);
88 
89  instance->start_decomposition(true, true);
90 
91  return 0;
92 
93 }
94 
100 
101  N_Qubit_Decomposition* instance = reinterpret_cast<N_Qubit_Decomposition*>(ptr);
102 
103 
104  delete instance;
105 
106  return 0;
107 
108 }
109 
110 
111 
118 int iface_set_identical_blocks( void* ptr, int qbit, int identical_blocks ) {
119 
120  N_Qubit_Decomposition* instance = reinterpret_cast<N_Qubit_Decomposition*>(ptr);
121  return instance->set_identical_blocks( qbit, identical_blocks );
122 
123 }
124 
131 int iface_set_iteration_loops( void* ptr, int qbit, int iteration_loops ) {
132 
133  N_Qubit_Decomposition* instance = reinterpret_cast<N_Qubit_Decomposition*>(ptr);
134  return instance->set_iteration_loops( qbit, iteration_loops );
135 
136 }
137 
144 int iface_set_max_layer_num( void* ptr, int qbit, int max_layer_num ) {
145 
146  N_Qubit_Decomposition* instance = reinterpret_cast<N_Qubit_Decomposition*>(ptr);
147  return instance->set_max_layer_num( qbit, max_layer_num );
148 
149 }
150 
156 void iface_list_operations( void* ptr, int start_index ) {
157 
158  N_Qubit_Decomposition* instance = reinterpret_cast<N_Qubit_Decomposition*>(ptr);
159  return instance->list_operations( start_index );
160 
161 }
162 
167 int iface_get_operation_num( void* ptr ) {
168 
169  N_Qubit_Decomposition* instance = reinterpret_cast<N_Qubit_Decomposition*>(ptr);
170  return instance->get_operation_num();
171 
172 }
173 
174 
175 
186 int iface_get_operation( void* ptr, int n, int &op_type, int &target_qbit, int &control_qbit, double* parameters ) {
187 
188  operation_type type;
189 //printf("a %f, %f, %f\n", parameters[0], parameters[1], parameters[2] );
190  N_Qubit_Decomposition* instance = reinterpret_cast<N_Qubit_Decomposition*>(ptr);
191  int ret = instance->get_operation( n, type, target_qbit, control_qbit, parameters );
192 //printf("b %f, %f, %f\n", parameters[0], parameters[1], parameters[2] );
193  op_type = type;
194 
195  return ret;
196 
197 
198 }
199 
200 
207 int iface_set_verbose( void* ptr, bool verbose ) {
208 
209  N_Qubit_Decomposition* instance = reinterpret_cast<N_Qubit_Decomposition*>(ptr);
210  instance->set_verbose( verbose );
211 
212  return 0;
213 
214 }
215 
216 
223 int iface_set_optimalization_block( void* ptr, int optimalization_block ) {
224 
225  N_Qubit_Decomposition* instance = reinterpret_cast<N_Qubit_Decomposition*>(ptr);
226  instance->set_optimization_blocks( optimalization_block );
227 
228  return 0;
229 
230 }
231 
232 } // end extern C
void iface_list_operations(void *ptr, int start_index)
Call to list the operations giving the decomposition of the unitary.
int iface_get_operation_num(void *ptr)
Call to get the number of decomposing operations.
void * iface_new_N_Qubit_Decomposition(double *mtx_real, double *mtx_imag, int qbit_num, bool optimize_layer_num, int initial_guess_num)
Creates an instance of class N_Qubit_Decomposition and return with a void pointer pointing to the cla...
int iface_set_verbose(void *ptr, bool verbose)
Call to set the verbosity of the N_Qubit_Decomposition class.
int set_max_layer_num(int n, int max_layer_num_in)
Set the maximal number of layers used in the subdecomposition of the n-th qubit.
int iface_set_max_layer_num(void *ptr, int qbit, int max_layer_num)
Set the maximal number of layers used in the subdecomposition of the qbit-th qubit.
int iface_set_optimalization_block(void *ptr, int optimalization_block)
Call to set the number of blocks to be optimized in one shot.
int iface_start_decomposition(void *ptr)
Starts the decomposition of the unitary.
Header file for a methods of the QGD Python Interface.
matrix_size
Definition: example.py:41
Umtx
The unitary to be decomposed.
Definition: example.py:44
int iface_set_identical_blocks(void *ptr, int qbit, int identical_blocks)
Set the number of identical successive blocks during the subdecomposition of the qbit-th qubit.
int Power_of_2(int n)
Calculates the n-th power of 2.
Definition: common.cpp:81
void start_decomposition(bool finalize_decomp, bool prepare_export)
Start the disentanglig process of the unitary.
Class to store data of complex arrays and its properties.
Definition: matrix.h:12
int qbit_num
Definition: example.py:38
int iface_delete_N_Qubit_Decomposition(void *ptr)
Call to deallocate the N_Qubit_Decomposition class.
int iface_get_operation(void *ptr, int n, int &op_type, int &target_qbit, int &control_qbit, double *parameters)
Call to get the n-th optimized operation.
guess_type
Type definition of the types of the initial guess.
int get_operation(unsigned int n, operation_type &type, int &target_qbit, int &control_qbit, double *parameters)
Call to prepare the optimized operations to export.
int iface_set_iteration_loops(void *ptr, int qbit, int iteration_loops)
Set the number of iteration loops during the subdecomposition of the qbit-th qubit.
int get_operation_num()
Call to get the number of operations grouped in the class.
void set_verbose(bool verbose_in)
Call to set the verbose attribute to true or false.
int set_iteration_loops(int n, int iteration_loops_in)
Set the number of iteration loops during the subdecomposition of the n-th qubit.
operation_type
Type definition of operation types (also generalized for decomposition classes derived from the class...
Definition: Operation.h:33
Header file for a class to determine the decomposition of a unitary into a sequence of CNOT and U3 op...
void list_operations(int start_index)
Call to print the operations decomposing the initial unitary.
int set_identical_blocks(int n, int identical_blocks_in)
Set the number of identical successive blocks during the subdecomposition of the n-th qubit.
void set_optimization_blocks(int optimization_block_in)
Call to set the number of operation blocks to be optimized in one shot.
A class to determine the decomposition of a unitary into a sequence of CNOT and U3 operations.