Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
Gates_block.h
Go to the documentation of this file.
1 /*
2 Created on Fri Jun 26 14:13:26 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 @author: Peter Rakyta, Ph.D.
18 */
23 #ifndef GATES_BLOCK_H
24 #define GATES_BLOCK_H
25 
26 #include <vector>
27 #include <map>
28 #include "common.h"
29 #include "matrix_real.h"
30 #include "CROT.h"
31 #include "Gate.h"
32 
33 #ifdef __DFE__
34 #include "common_DFE.h"
35 #endif
36 
37 
41 class Gates_block : public Gate {
42 
43 
44 protected:
46  std::vector<Gate*> gates;
48  int layer_num;
49 
50 
52 
54  bool fragmented;
58  std::vector< std::vector<int>> involved_qbits;
59  std::vector<int> block_end;
60  std::vector<int> block_type;
62 
63 public:
64 
68 Gates_block();
69 
74 Gates_block(int qbit_num_in);
75 
76 
80 virtual ~Gates_block();
81 
85 void release_gates();
86 
90 void release_gate( int idx);
91 
98 
105 Matrix get_matrix( Matrix_real& parameters, int parallel );
106 
107 
114 void apply_to_list( Matrix_real& parameters, std::vector<Matrix>& inputs, int parallel );
115 
122 virtual void apply_to( Matrix_real& parameters_mtx, Matrix& input, int parallel=0 );
123 
124 
125 
126 
131 virtual void apply_from_right( Matrix_real& parameters_mtx, Matrix& input );
132 
133 
140 virtual std::vector<Matrix> apply_derivate_to( Matrix_real& parameters_mtx, Matrix& input, int parallel );
141 
146 void add_u1(int target_qbit);
147 
152 void add_u1_to_front(int target_qbit);
153 
158 void add_u2(int target_qbit);
159 
164 void add_u2_to_front(int target_qbit);
165 
173 void add_u3(int target_qbit);
174 
182 void add_u3_to_front(int target_qbit);
183 
188 void add_rx(int target_qbit);
189 
194 void add_rx_to_front(int target_qbit);
195 
200 void add_r(int target_qbit);
201 
206 void add_r_to_front(int target_qbit);
207 
212 void add_ry(int target_qbit);
213 
218 void add_ry_to_front(int target_qbit);
219 
220 
221 
222 
228 void add_cry(int target_qbit, int control_qbit);
229 
236 
242 void add_cz_nu(int target_qbit, int control_qbit);
243 
244 
245 
252 
253 
254 
259 void add_rz(int target_qbit);
260 
265 void add_rz_to_front(int target_qbit);
266 
267 
268 
274 void add_cnot( int target_qbit, int control_qbit);
275 
276 
277 
284 
285 
291 void add_crot( int target_qbit, int control_qbit, crot_type subtype_in);
292 
293 
294 
300 void add_crot_to_front( int target_qbit, int control_qbit, crot_type subtype_in );
301 
307 void add_cz( int target_qbit, int control_qbit);
308 
309 
310 
316 void add_cz_to_front( int target_qbit, int control_qbit );
317 
318 
324 void add_ch( int target_qbit, int control_qbit);
325 
326 
327 
333 void add_ch_to_front( int target_qbit, int control_qbit );
334 
339 void add_h(int target_qbit);
340 
345 void add_h_to_front(int target_qbit);
346 
347 
352 void add_x(int target_qbit);
353 
358 void add_x_to_front(int target_qbit);
359 
360 
365 void add_y(int target_qbit);
366 
371 void add_y_to_front(int target_qbit);
372 
373 
378 void add_z(int target_qbit);
379 
384 void add_z_to_front(int target_qbit);
385 
386 
391 void add_t(int target_qbit);
392 
397 void add_t_to_front(int target_qbit);
398 
399 
404 void add_tdg(int target_qbit);
405 
410 void add_tdg_to_front(int target_qbit);
411 
412 
417 void add_sx(int target_qbit);
418 
423 void add_sx_to_front(int target_qbit);
424 
430 void add_syc( int target_qbit, int control_qbit);
431 
432 
433 
439 void add_syc_to_front( int target_qbit, int control_qbit );
440 
441 
442 
443 
447 void add_un();
448 
452 void add_un_to_front();
453 
454 
458 void add_on();
459 
463 void add_on_to_front();
464 
465 
469 void add_composite();
470 
475 
476 
482 void add_adaptive(int target_qbit, int control_qbit);
483 
490 
491 
496 void add_gates( std::vector<Gate*> gates_in );
497 
498 
503 void add_gates_to_front( std::vector<Gate*> gates_in );
504 
505 
510 void add_gate( Gate* gate );
511 
516 void add_gate_to_front( Gate* gate );
517 
518 
524 void insert_gate( Gate* gate, int idx );
525 
526 
531 void add_gate_nums( std::map<std::string, int>& gate_nums );
532 
533 
538 std::map<std::string, int> get_gate_nums();
539 
540 
545 int get_parameter_num();
546 
547 
548 
553 int get_gate_num();
554 
555 
561 void list_gates( const Matrix_real &parameters, int start_index );
562 
563 
569 Gates_block* create_remapped_circuit( const std::map<int, int>& qbit_map );
570 
571 
578 Gates_block* create_remapped_circuit( const std::map<int, int>& qbit_map, const int qbit_num_ );
579 
580 
585 virtual void reorder_qubits( std::vector<int> qbit_list );
586 
587 
592 std::vector<int> get_involved_qubits();
593 
598 std::vector<Gate*> get_gates();
599 
600 
605 Gate* get_gate(int idx);
606 
611 void combine(Gates_block* op_block);
612 
613 
618 virtual void set_qbit_num( int qbit_num_in );
619 
620 
625 virtual Gates_block* clone();
626 
632 int extract_gates( Gates_block* op_block );
633 
634 
635 
641 
647 bool contains_adaptive_gate(int idx);
648 
649 
650 
658 Matrix get_reduced_density_matrix( Matrix_real& parameters_mtx, Matrix& input_state, matrix_base<int>& qbit_list_subset );
659 
660 
661 
669 double get_second_Renyi_entropy( Matrix_real& parameters_mtx, Matrix& input_state, matrix_base<int>& qbit_list );
670 
671 
672 
677 void determine_parents( Gate* gate );
678 
679 
684 void determine_children( Gate* gate );
685 
686 
687 
689 
690 void fragment_circuit();
691 void get_parameter_max(Matrix_real &range_max);
692 
694 
695 #ifdef __DFE__
696 
701 DFEgate_kernel_type* convert_to_DFE_gates_with_derivates( Matrix_real& parameters_mtx, int& gatesNum, int& gateSetNum, int& redundantGateSets, bool only_derivates=false );
702 
707 void adjust_parameters_for_derivation( DFEgate_kernel_type* DFEgates, const int gatesNum, int& gate_idx, int& gate_set_index );
708 
713 DFEgate_kernel_type* convert_to_batched_DFE_gates( std::vector<Matrix_real>& parameters_mtx_vec, int& gatesNum, int& gateSetNum, int& redundantGateSets );
714 
715 
716 
721 DFEgate_kernel_type* convert_to_DFE_gates( Matrix_real& parameters_mtx, int& gatesNum );
722 
727 void convert_to_DFE_gates( const Matrix_real& parameters_mtx, DFEgate_kernel_type* DFEgates, int& start_index );
728 
729 #endif
730 
731 
732 #ifdef __GROQ__
733 
734 void extract_gate_kernels_target_and_control_qubits(std::vector<Matrix> &u3_qbit, std::vector<int> &target_qbit, std::vector<int> &control_qbit, Matrix_real& parameters_mtx);
735 
736 #endif
737 
738 
743 
744 
745 
750 
751 
756 
757 
764 
765 };
766 
767 
768 
773 void export_gate_list_to_binary(Matrix_real& parameters, Gates_block* gates_block, const std::string& filename, int verbosity=3);
774 
779 void export_gate_list_to_binary(Matrix_real& parameters, Gates_block* gates_block, FILE* pFile, int verbosity=3);
780 
781 
786 Gates_block* import_gate_list_from_binary(Matrix_real& parameters, const std::string& filename, int verbosity=3);
787 
788 
793 Gates_block* import_gate_list_from_binary(Matrix_real& parameters, FILE* pFile, int verbosity=3);
794 
796 bool is_qbit_present(std::vector<int> involved_qbits, int new_qbit, int num_of_qbits);
798 
799 
800 
807 Matrix_real reverse_parameters( const Matrix_real& v_in, std::vector<Gate*>::iterator gates_it, int num_of_gates );
808 
809 
816 Matrix_real inverse_reverse_parameters( const Matrix_real& v_in, std::vector<Gate*>::iterator gates_it, int num_of_gates );
817 
818 #endif //GATES_BLOCK,
819 
Gates_block * get_flat_circuit()
Method to generate a flat circuit.
void add_adaptive(int target_qbit, int control_qbit)
Append a Adaptive gate to the list of gates.
void add_composite_to_front()
Add a Composite gate to the front of the list of gates.
void add_cz_nu(int target_qbit, int control_qbit)
Append a CZ_NU gate to the list of gates.
void apply_to_list(Matrix_real &parameters, std::vector< Matrix > &inputs, int parallel)
Call to apply the gate on the input array/matrix by U3*input.
Header file for a class for the representation of general gate operations.
void release_gate(int idx)
Call to release one gate in the list.
void determine_parents(Gate *gate)
Call to obtain the parent gates in the circuit.
void add_z_to_front(int target_qbit)
Add a Z gate to the front of the list of gates.
void add_h(int target_qbit)
Append a Hadamard gate to the list of gates.
virtual Matrix get_matrix()
Call to retrieve the operation matrix.
Definition: Gate.cpp:129
std::map< std::string, int > get_gate_nums()
Call to get the number of the individual gate types in the list of gates.
Matrix_real reverse_parameters(const Matrix_real &v_in, std::vector< Gate *>::iterator gates_it, int num_of_gates)
Call to reverse the order of the parameters in an array.
void fragment_circuit()
void add_x(int target_qbit)
Append a X gate to the list of gates.
int control_qbit
The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled operations...
Definition: Gate.h:87
void add_cz_nu_to_front(int target_qbit, int control_qbit)
Add a CZ_NU gate to the front of the list of gates.
void add_gate(Gate *gate)
Append a general gate to the list of gates.
void add_u2(int target_qbit)
Append a U2 gate to the list of gates.
std::vector< int > get_involved_qubits()
Call to get the qubits involved in the gates stored in the block of gates.
void add_cnot_to_front(int target_qbit, int control_qbit)
Add a C_NOT gate gate to the front of the list of gates.
int max_fusion
maximal number of qubits in partitions
Definition: Gates_block.h:57
void add_u1(int target_qbit)
Append a U1 gate to the list of gates.
void add_on_to_front()
Add a OUN gate to the front of the list of gates.
Header file for a class representing a controlled rotation gate around the Y axis.
int target_qbit
The index of the qubit on which the operation acts (target_qbit >= 0)
Definition: Gate.h:85
std::vector< Gate * > get_gates()
Call to get the gates stored in the class.
Gates_block * create_remapped_circuit(const std::map< int, int > &qbit_map)
Call to create a new circuit with remapped qubits.
void release_gates()
Call to release the stored gates.
virtual void reorder_qubits(std::vector< int > qbit_list)
Call to reorder the qubits in the matrix of the gates (Obsolete function)
std::vector< int > block_type
Definition: Gates_block.h:60
void get_parameter_max(Matrix_real &range_max)
void add_r(int target_qbit)
Append a R gate to the list of gates.
std::vector< std::vector< int > > involved_qbits
Definition: Gates_block.h:58
int layer_num
number of gate layers
Definition: Gates_block.h:48
void add_cnot(int target_qbit, int control_qbit)
Append a CNOT gate gate to the list of gates.
Gates_block * import_gate_list_from_binary(Matrix_real &parameters, const std::string &filename, int verbosity=3)
?????????
bool fragmented
boolean variable indicating whether the circuit was already partitioned or not
Definition: Gates_block.h:54
void add_cz_to_front(int target_qbit, int control_qbit)
Add a CZ gate gate to the front of the list of gates.
virtual void apply_to(Matrix_real &parameters_mtx, Matrix &input, int parallel=0)
Call to apply the gate on the input array/matrix Gates_block*input.
void add_crot(int target_qbit, int control_qbit, crot_type subtype_in)
Append a CNOT gate gate to the list of gates.
void add_rz_to_front(int target_qbit)
Add a RZ gate to the front of the list of gates.
void add_r_to_front(int target_qbit)
Add a R gate to the front of the list of gates.
void add_un_to_front()
Add a UN gate to the front of the list of gates.
int get_gate_num()
Call to get the number of gates grouped in the class.
void add_h_to_front(int target_qbit)
Add a Hadamard gate to the front of the list of gates.
std::vector< Gate * > gates
The list of stored gates.
Definition: Gates_block.h:46
void add_x_to_front(int target_qbit)
Add a X gate to the front of the list of gates.
void add_sx_to_front(int target_qbit)
Add a SX gate to the front of the list of gates.
std::vector< int > block_end
Definition: Gates_block.h:59
void add_y_to_front(int target_qbit)
Add a Y gate to the front of the list of gates.
void add_syc(int target_qbit, int control_qbit)
Append a Sycamore gate (i.e.
int get_parameter_num()
Call to get the number of free parameters.
double get_second_Renyi_entropy(Matrix_real &parameters_mtx, Matrix &input_state, matrix_base< int > &qbit_list)
Call to evaluate the seconf Rényi entropy.
void add_syc_to_front(int target_qbit, int control_qbit)
Add a Sycamore gate (i.e.
void add_ch(int target_qbit, int control_qbit)
Append a CH gate (i.e.
void add_ch_to_front(int target_qbit, int control_qbit)
Add a CH gate (i.e.
void add_composite()
Append a Composite gate to the list of gates.
int fragmentation_type
Definition: Gates_block.h:55
void add_u2_to_front(int target_qbit)
Add a U2 gate to the front of the list of gates.
void add_u3(int target_qbit)
Append a U3 gate to the list of gates.
void add_rz(int target_qbit)
Append a RZ gate to the list of gates.
void add_y(int target_qbit)
Append a Y gate to the list of gates.
void reset_dependency_graph()
Method to reset the dependency graph of the gates in the circuit.
void combine(Gates_block *op_block)
Call to append the gates of an gate block to the current block.
void add_ry(int target_qbit)
Append a RY gate to the list of gates.
void add_on()
Append a ON gate to the list of gates.
virtual std::vector< Matrix > apply_derivate_to(Matrix_real &parameters_mtx, Matrix &input, int parallel)
Call to evaluate the derivate of the circuit on an inout with respect to all of the free parameters...
void determine_children(Gate *gate)
Call to obtain the child gates in the circuit.
void add_u1_to_front(int target_qbit)
Add a U1 gate to the front of the list of gates.
int extract_gates(Gates_block *op_block)
Call to extract the gates stored in the class.
void add_cry_to_front(int target_qbit, int control_qbit)
Add a CRY gate to the front of the list of gates.
void add_crot_to_front(int target_qbit, int control_qbit, crot_type subtype_in)
Add a C_NOT gate gate to the front of the list of gates.
virtual Gates_block * clone()
Create a clone of the present class.
virtual void apply_from_right(Matrix_real &parameters_mtx, Matrix &input)
Call to apply the gate on the input array/matrix by input*CNOT.
bool contains_adaptive_gate()
Call to determine, whether the circuit contains daptive gate or not.
Class to store data of complex arrays and its properties.
Definition: matrix.h:38
void add_gates_to_front(std::vector< Gate *> gates_in)
Add an array of gates to the front of the list of gates.
void add_gate_to_front(Gate *gate)
Add an gate to the front of the list of gates.
void add_z(int target_qbit)
Append a Z gate to the list of gates.
void add_cry(int target_qbit, int control_qbit)
Append a CRY gate to the list of gates.
Gates_block()
Default constructor of the class.
Definition: Gates_block.cpp:64
void add_rx_to_front(int target_qbit)
Add a RX gate to the front of the list of gates.
void add_tdg_to_front(int target_qbit)
Add a Tdg gate to the front of the list of gates.
A class responsible for grouping two-qubit (CNOT,CZ,CH) and one-qubit gates into layers.
Definition: Gates_block.h:41
void add_tdg(int target_qbit)
Append a Tdg gate to the list of gates.
void add_u3_to_front(int target_qbit)
Add a U3 gate to the front of the list of gates.
void add_cz(int target_qbit, int control_qbit)
Append a CZ gate gate to the list of gates.
Fixed point data related to a gate operation.
Definition: common_DFE.h:61
Base class for the representation of general gate operations.
Definition: Gate.h:73
bool is_qbit_present(std::vector< int > involved_qbits, int new_qbit, int num_of_qbits)
Call to check whether the given qubit is involved in the sub-circuit or not.
void add_gate_nums(std::map< std::string, int > &gate_nums)
Call to add the number of the individual gate types in the circuit to the map given in the argument...
void add_ry_to_front(int target_qbit)
Add a RY gate to the front of the list of gates.
virtual ~Gates_block()
Destructor of the class.
Header file for commonly used functions and wrappers to CBLAS functions.
void add_rx(int target_qbit)
Append a RX gate to the list of gates.
void add_un()
Append a UN gate to the list of gates.
void insert_gate(Gate *gate, int idx)
Call to insert a gate at a given position.
virtual Matrix_real extract_parameters(Matrix_real &parameters)
Call to extract parameters from the parameter array corresponding to the circuit, in which the gate i...
void reset_parameter_start_indices()
Method to reset the parameter start indices of gate operations incorporated in the circuit...
Matrix_real inverse_reverse_parameters(const Matrix_real &v_in, std::vector< Gate *>::iterator gates_it, int num_of_gates)
Call to inverse-reverse the order of the parameters in an array.
void add_t_to_front(int target_qbit)
Add a T gate to the front of the list of gates.
Header file for DFE support in unitary simulation.
Gate * get_gate(int idx)
Call to get the gates stored in the class.
void list_gates(const Matrix_real &parameters, int start_index)
Call to print the list of gates stored in the block of gates for a specific set of parameters...
Matrix get_reduced_density_matrix(Matrix_real &parameters_mtx, Matrix &input_state, matrix_base< int > &qbit_list_subset)
Call to evaluate the reduced densiy matrix.
void add_adaptive_to_front(int target_qbit, int control_qbit)
Add a Adaptive gate to the front of the list of gates.
void add_gates(std::vector< Gate *> gates_in)
Append a list of gates to the list of gates.
void add_t(int target_qbit)
Append a T gate to the list of gates.
Class to store data of complex arrays and its properties.
Definition: matrix_real.h:39
crot_type
Definition: CROT.h:34
void export_gate_list_to_binary(Matrix_real &parameters, Gates_block *gates_block, const std::string &filename, int verbosity=3)
?????????
void add_sx(int target_qbit)
Append a SX gate to the list of gates.
virtual void set_qbit_num(int qbit_num_in)
Set the number of qubits spanning the matrix of the gates stored in the block of gates.