Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
Gate.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 GATE_H
24 #define GATE_H
25 
26 #include <vector>
27 #include "common.h"
28 #include "matrix.h"
29 #include "matrix_float.h"
30 #include "logging.h"
31 #include "matrix_real.h"
32 #include "matrix_real_float.h"
33 #include "matrix_any.h"
34 #include "matrix_real_any.h"
35 #include <utility>
36 
37 
39 typedef enum gate_type {GENERAL_OPERATION=1,
80 
81 
82 
86 class Gate : public logging {
87 
88 
89 protected:
90 
92  std::string name;
94  int qbit_num;
102  std::vector<int> target_qbits;
104  std::vector<int> control_qbits;
112  std::vector<Gate*> parents;
114  std::vector<Gate*> children;
115 
116 private:
117 
123 
124 public:
125 
130 Gate();
131 
135 virtual ~Gate();
136 
137 
143 Gate(int qbit_num_in);
144 
152 Gate(int qbit_num_in, const std::vector<int>& target_qbits_in, const std::vector<int>& control_qbits_in = std::vector<int>());
153 
160 virtual Matrix get_matrix( Matrix_real& parameters, int parallel );
161 
166 
170 virtual Matrix get_matrix();
171 
175 virtual Matrix get_matrix( int parallel );
176 
183 virtual Matrix_float get_matrix( Matrix_real_float& parameters, int parallel );
184 
190 virtual void apply_to_list( std::vector<Matrix>& inputs, int parallel );
191 
192 
199 virtual void apply_to_list( Matrix_real& parameters_mtx, std::vector<Matrix>& inputs, int parallel );
200 
206 virtual void apply_to_list( std::vector<Matrix_float>& inputs, int parallel );
207 
214 virtual void apply_to_list( Matrix_real_float& parameters_mtx, std::vector<Matrix_float>& inputs, int parallel );
215 
221 virtual void apply_to( Matrix& input, int parallel );
222 
228 virtual void apply_to( Matrix_float& input, int parallel );
229 
236 virtual void apply_to( Matrix_real& parameter_mtx, Matrix& input, int parallel );
237 
244 virtual void apply_to( Matrix_real_float& parameter_mtx, Matrix_float& input, int parallel );
245 
250 virtual void apply_to_inner( Matrix_real& parameter_mtx, const Matrix_real& precomputed_sincos, Matrix& input, int parallel );
251 
256 virtual void apply_to_inner( Matrix_real_float& parameter_mtx, const Matrix_real_float& precomputed_sincos, Matrix_float& input, int parallel );
257 
263 void apply_to( Matrix_any& input, int parallel );
264 
271 void apply_to( Matrix_real_any& parameter_mtx, Matrix_any& input, int parallel );
272 
273 
280 virtual std::vector<Matrix> apply_derivate_to( Matrix_real& parameters_mtx_in, Matrix& input, int parallel );
281 
282 virtual void apply_derivate_to( Matrix_real& parameters_mtx_in, Matrix& input, int parallel, std::vector<Matrix>& output );
283 
290 virtual std::vector<Matrix_float> apply_derivate_to( Matrix_real_float& parameters_mtx_in, Matrix_float& input, int parallel );
291 
292 virtual void apply_derivate_to( Matrix_real_float& parameters_mtx_in, Matrix_float& input, int parallel, std::vector<Matrix_float>& output );
293 
301 virtual std::vector<Matrix> apply_to_combined( Matrix_real& parameters_mtx_in, Matrix& input, int parallel );
302 
303 virtual void apply_to_combined( Matrix_real& parameters_mtx_in, Matrix& input, int parallel, std::vector<Matrix>& output );
304 
312 virtual std::vector<Matrix_float> apply_to_combined( Matrix_real_float& parameters_mtx_in, Matrix_float& input, int parallel );
313 
314 virtual void apply_to_combined( Matrix_real_float& parameters_mtx_in, Matrix_float& input, int parallel, std::vector<Matrix_float>& output );
315 
319 virtual std::vector<Matrix> apply_to_combined_inner( Matrix_real& parameters_mtx_in, const Matrix_real& precomputed_sincos, Matrix& input, int parallel );
320 
321 virtual void apply_to_combined_inner( Matrix_real& parameters_mtx_in, const Matrix_real& precomputed_sincos, Matrix& input, int parallel, std::vector<Matrix>& output );
322 
326 virtual std::vector<Matrix_float> apply_to_combined_inner( Matrix_real_float& parameters_mtx_in, const Matrix_real_float& precomputed_sincos, Matrix_float& input, int parallel );
327 
328 virtual void apply_to_combined_inner( Matrix_real_float& parameters_mtx_in, const Matrix_real_float& precomputed_sincos, Matrix_float& input, int parallel, std::vector<Matrix_float>& output );
329 
330 
335 virtual void apply_from_right( Matrix& input );
336 
341 virtual void apply_from_right( Matrix_float& input );
342 
348 virtual void apply_from_right( Matrix_real& parameter_mtx, Matrix& input );
349 
355 virtual void apply_from_right( Matrix_real_float& parameter_mtx, Matrix_float& input );
356 
360 virtual void apply_from_right_inner( Matrix_real& parameter_mtx, const Matrix_real& precomputed_sincos, Matrix& input );
361 
365 virtual void apply_from_right_inner( Matrix_real_float& parameter_mtx, const Matrix_real_float& precomputed_sincos, Matrix_float& input );
366 
371 
376 
382 void set_matrix( Matrix input );
383 
384 void set_matrix( Matrix_float input );
385 
386 
391 void set_control_qbit(int control_qbit_in);
392 
397 void set_target_qbit(int target_qbit_in);
398 
403 void set_control_qbits(const std::vector<int>& control_qbits_in);
404 
409 void set_target_qbits(const std::vector<int>& target_qbits_in);
410 
415 virtual void set_qbit_num( int qbit_num_in );
416 
421 virtual void reorder_qubits( std::vector<int> qbit_list );
422 
423 
428 int get_target_qbit();
429 
430 
435 int get_control_qbit();
436 
441 std::vector<int> get_target_qbits() const;
442 
447 std::vector<int> get_control_qbits() const;
448 
453 virtual std::vector<int> get_involved_qubits(bool only_target=false);
454 
459 void add_child( Gate* child );
460 
461 
466 void add_parent( Gate* parent );
467 
468 
472 void clear_children();
473 
474 
478 void clear_parents();
479 
484 virtual int get_parameter_num();
485 
486 
492 
497 int get_qbit_num();
498 
499 
504 void set_parameter_start_idx(int start_idx);
505 
506 
511 void set_parents( std::vector<Gate*>& parents_ );
512 
513 
518 void set_children( std::vector<Gate*>& children_ );
519 
520 
521 
526 std::vector<Gate*> get_parents();
527 
528 
533 std::vector<Gate*> get_children();
534 
535 
541 
542 
547 std::string get_name();
548 
553 virtual Gate* clone();
554 
563 virtual Matrix gate_kernel(const Matrix_real& precomputed_sincos);
564 virtual Matrix_float gate_kernel(const Matrix_real_float& precomputed_sincos);
565 virtual Matrix inverse_gate_kernel(const Matrix_real& precomputed_sincos);
566 virtual Matrix_float inverse_gate_kernel(const Matrix_real_float& precomputed_sincos);
567 virtual Matrix derivative_kernel(const Matrix_real& precomputed_sincos, int param_idx);
568 virtual Matrix_float derivative_kernel(const Matrix_real_float& precomputed_sincos, int param_idx);
569 virtual void gate_kernel_to(const Matrix_real& precomputed_sincos, Matrix& output);
570 virtual void gate_kernel_to(const Matrix_real_float& precomputed_sincos, Matrix_float& output);
571 virtual void inverse_gate_kernel_to(const Matrix_real& precomputed_sincos, Matrix& output);
572 virtual void inverse_gate_kernel_to(const Matrix_real_float& precomputed_sincos, Matrix_float& output);
573 virtual void derivative_kernel_to(const Matrix_real& precomputed_sincos, int param_idx, Matrix& output);
574 virtual void derivative_kernel_to(const Matrix_real_float& precomputed_sincos, int param_idx, Matrix_float& output);
575 virtual void derivative_aux_kernel_to(const Matrix_real& precomputed_sincos, int param_idx, Matrix& output);
576 virtual void derivative_aux_kernel_to(const Matrix_real_float& precomputed_sincos, int param_idx, Matrix_float& output);
577 virtual Matrix derivative_aux_kernel(const Matrix_real& precomputed_sincos, int param_idx);
578 virtual Matrix_float derivative_aux_kernel(const Matrix_real_float& precomputed_sincos, int param_idx);
579 
586 virtual std::vector<double> get_parameter_multipliers() const;
587 
592 static Matrix calc_one_qubit_u3(double ThetaOver2 = 0.0, double Phi = 0.0, double Lambda = 0.0);
593 static Matrix_float calc_one_qubit_u3(float ThetaOver2, float Phi, float Lambda);
594 
601 
608 
609 
610 protected:
617 
624 
625 std::vector<Matrix> apply_derivative_to_precomputed(const Matrix_real& precomputed_sincos, Matrix& input, int parallel);
626 
627 void apply_derivative_to_precomputed(const Matrix_real& precomputed_sincos, Matrix& input, int parallel, std::vector<Matrix>& output, size_t offset = 0, bool resize_output = true);
628 
629 std::vector<Matrix_float> apply_derivative_to_precomputed(const Matrix_real_float& precomputed_sincos, Matrix_float& input, int parallel);
630 
631 void apply_derivative_to_precomputed(const Matrix_real_float& precomputed_sincos, Matrix_float& input, int parallel, std::vector<Matrix_float>& output, size_t offset = 0, bool resize_output = true);
632 
641 void apply_kernel_to( Matrix& u3_1qbit, Matrix& input, bool deriv=false, int parallel=0, const Matrix* alt_kernel=nullptr );
642 
646 void apply_kernel_to( Matrix_float& u3_1qbit, Matrix_float& input, bool deriv=false, int parallel=0, const Matrix_float* alt_kernel=nullptr );
647 
648 
649 
656 void apply_kernel_from_right( Matrix& u3_1qbit, Matrix& input, const Matrix* alt_kernel=nullptr );
657 
658 void apply_kernel_from_right( Matrix_float& u3_1qbit, Matrix_float& input, const Matrix_float* alt_kernel=nullptr );
659 
660 
661 };
662 
663 #endif //GATE
std::vector< Gate * > parents
list of parent gates to be applied in the circuit prior to this current gate
Definition: Gate.h:112
Matrix_real compute_precomputed_sincos(const Matrix_real &parameters) const
Public wrapper to precompute gate-local sin/cos pairs.
Definition: Gate.cpp:1017
Gate()
Default constructor of the class.
Definition: Gate.cpp:121
Header file of single-precision complex array storage with automatic and thread safe reference counti...
virtual Matrix inverse_gate_kernel(const Matrix_real &precomputed_sincos)
Definition: Gate.cpp:2560
bool matrix_alloc_float_valid
Definition: Gate.h:122
void set_control_qbits(const std::vector< int > &control_qbits_in)
Call to set the control qubits for the gate operation.
Definition: Gate.cpp:1097
Class to store single-precision real arrays and properties.
virtual void gate_kernel_to(const Matrix_real &precomputed_sincos, Matrix &output)
Definition: Gate.cpp:2593
virtual Matrix derivative_aux_kernel(const Matrix_real &precomputed_sincos, int param_idx)
Definition: Gate.cpp:1415
void add_child(Gate *child)
Call to add a child gate to the current gate.
Definition: Gate.cpp:1264
virtual Matrix get_matrix()
Retrieve the gate matrix for zero-parameter gates (no parameters).
Definition: Gate.cpp:283
void clear_children()
Call to erase data on children.
Definition: Gate.cpp:1279
std::vector< int > target_qbits
Vector of target qubit indices (for multi-qubit gates)
Definition: Gate.h:102
int control_qbit
The index of the qubit which acts as a control qubit (control_qbit >= 0) in controlled operations...
Definition: Gate.h:100
virtual std::vector< Matrix > apply_derivate_to(Matrix_real &parameters_mtx_in, Matrix &input, int parallel)
Call to evaluate the derivate of the circuit on an inout with respect to all of the free parameters...
Definition: Gate.cpp:784
virtual Gate * clone()
Call to create a clone of the present class.
Definition: Gate.cpp:1351
void set_children(std::vector< Gate *> &children_)
Call to set the children of the current gate.
Definition: Gate.cpp:2523
void apply_kernel_from_right(Matrix &u3_1qbit, Matrix &input, const Matrix *alt_kernel=nullptr)
Call to apply the gate kernel on the input state or unitary from right (no AVX support) ...
Definition: Gate.cpp:2082
virtual void set_qbit_num(int qbit_num_in)
Set the number of qubits spanning the matrix of the operation.
Definition: Gate.cpp:244
int target_qbit
The index of the qubit on which the operation acts (target_qbit >= 0)
Definition: Gate.h:98
virtual ~Gate()
Destructor of the class.
Definition: Gate.cpp:237
virtual std::vector< Matrix > apply_to_combined_inner(Matrix_real &parameters_mtx_in, const Matrix_real &precomputed_sincos, Matrix &input, int parallel)
Internal combined forward+derivative entry with precomputed sin/cos.
Definition: Gate.cpp:832
Header file for a class containing basic methods for setting up the verbosity level.
int matrix_size
The size N of the NxN matrix associated with the operations.
Definition: Gate.h:106
static Matrix calc_one_qubit_u3(double ThetaOver2=0.0, double Phi=0.0, double Lambda=0.0)
Build a 2x2 U3 kernel from angles (theta/2, phi, lambda).
Definition: Gate.cpp:2650
Non-owning carrier that references either Matrix (complex128) or Matrix_float (complex64) without dat...
Definition: matrix_any.h:30
int parameter_start_idx
the index in the parameter array (corrensponding to the encapsulated circuit) where the gate paramete...
Definition: Gate.h:110
Non-owning carrier that can reference either Matrix_real or Matrix_real_float.
virtual void apply_from_right_inner(Matrix_real &parameter_mtx, const Matrix_real &precomputed_sincos, Matrix &input)
Internal right-apply entry that consumes already precomputed sin/cos values.
Definition: Gate.cpp:977
virtual void apply_to_inner(Matrix_real &parameter_mtx, const Matrix_real &precomputed_sincos, Matrix &input, int parallel)
Internal apply entry that consumes already precomputed sin/cos values.
Definition: Gate.cpp:549
A class containing basic methods for setting up the verbosity level.
Definition: logging.h:43
gate_type type
The type of the operation (see enumeration gate_type)
Definition: Gate.h:96
virtual void apply_from_right(Matrix &input)
Call to apply the gate on the input array/matrix by input*Gate.
Definition: Gate.cpp:919
std::vector< int > get_control_qbits() const
Call to get the vector of control qubits.
Definition: Gate.cpp:1135
virtual void derivative_kernel_to(const Matrix_real &precomputed_sincos, int param_idx, Matrix &output)
Definition: Gate.cpp:2617
gate_type get_type()
Call to get the type of the operation.
Definition: Gate.cpp:1333
Header file of complex array storage array with automatic and thread safe reference counting...
virtual void inverse_gate_kernel_to(const Matrix_real &precomputed_sincos, Matrix &output)
Definition: Gate.cpp:2605
void set_control_qbit(int control_qbit_in)
Call to set the control qubit for the gate operation.
Definition: Gate.cpp:1054
void set_parameter_start_idx(int start_idx)
Call to set the starting index of the parameters in the parameter array corresponding to the circuit ...
Definition: Gate.cpp:2500
std::vector< Gate * > get_parents()
Call to get the parents of the current gate.
Definition: Gate.cpp:1301
int get_parameter_start_idx()
Call to get the starting index of the parameters in the parameter array corresponding to the circuit ...
Definition: Gate.cpp:2535
void add_parent(Gate *parent)
Call to add a parent gate to the current gate.
Definition: Gate.cpp:1247
void set_target_qbit(int target_qbit_in)
Call to set the target qubit for the gate operation.
Definition: Gate.cpp:1076
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...
Definition: Gate.cpp:2679
void clear_parents()
Call to erase data on parents.
Definition: Gate.cpp:1289
Matrix_float matrix_alloc_float
Float32 matrix of the operation, when provided directly.
Definition: Gate.h:121
void set_matrix(Matrix input)
Call to set the stored matrix in the operation.
Definition: Gate.cpp:1036
std::vector< Gate * > get_children()
Call to get the children of the current gate.
Definition: Gate.cpp:1312
std::vector< int > get_target_qbits() const
Call to get the vector of target qubits.
Definition: Gate.cpp:1143
Double-precision complex matrix (float64).
Definition: matrix.h:38
std::vector< int > control_qbits
Vector of control qubit indices (for multi-qubit gates)
Definition: Gate.h:104
virtual int get_parameter_num()
Call to get the number of free parameters.
Definition: Gate.cpp:1324
virtual Matrix gate_kernel(const Matrix_real &precomputed_sincos)
Compute the gate kernel matrix from precomputed trigonometric values.
Definition: Gate.cpp:2546
std::string name
A string labeling the gate operation.
Definition: Gate.h:92
Single-precision complex matrix (float32).
Definition: matrix_float.h:41
virtual std::vector< Matrix > apply_to_combined(Matrix_real &parameters_mtx_in, Matrix &input, int parallel)
Combined forward + derivative application with shared precomputed trig cache.
Definition: Gate.cpp:816
std::vector< Gate * > children
list of child gates to be applied after this current gate
Definition: Gate.h:114
int get_target_qbit()
Call to get the index of the target qubit.
Definition: Gate.cpp:1203
Base class for the representation of general gate operations.
Definition: Gate.h:86
void set_target_qbits(const std::vector< int > &target_qbits_in)
Call to set the target qubits for the gate operation.
Definition: Gate.cpp:1116
std::string get_name()
Call to get the name label of the gate.
Definition: Gate.cpp:2740
void apply_kernel_to(Matrix &u3_1qbit, Matrix &input, bool deriv=false, int parallel=0, const Matrix *alt_kernel=nullptr)
Call to apply the gate kernel on the input state or unitary with optional AVX support.
Definition: Gate.cpp:1478
virtual Matrix derivative_kernel(const Matrix_real &precomputed_sincos, int param_idx)
Definition: Gate.cpp:1371
std::vector< Matrix > apply_derivative_to_precomputed(const Matrix_real &precomputed_sincos, Matrix &input, int parallel)
Call to evaluate the derivate of the circuit on an inout with respect to all of the free parameters...
Definition: Gate.cpp:636
int parameter_num
the number of free parameters of the operation
Definition: Gate.h:108
Header file for commonly used functions and wrappers to CBLAS functions.
virtual void apply_to_list(std::vector< Matrix > &inputs, int parallel)
Call to apply the gate on a list of inputs.
Definition: Gate.cpp:320
void set_parents(std::vector< Gate *> &parents_)
Call to set the parents of the current gate.
Definition: Gate.cpp:2511
virtual void derivative_aux_kernel_to(const Matrix_real &precomputed_sincos, int param_idx, Matrix &output)
Definition: Gate.cpp:2629
int qbit_num
number of qubits spanning the matrix of the operation
Definition: Gate.h:94
Matrix_real precompute_sincos(const Matrix_real &parameters) const
Precompute sin/cos pairs for each gate-local parameter.
Definition: Gate.cpp:1431
int get_qbit_num()
Call to get the number of qubits composing the unitary.
Definition: Gate.cpp:1342
gate_type
Type definition of operation types (also generalized for decomposition classes derived from the class...
Definition: Gate.h:39
virtual void apply_to(Matrix &input, int parallel)
Call to apply the gate on the input array/matrix.
Definition: Gate.cpp:433
virtual std::vector< double > get_parameter_multipliers() const
Returns the per-parameter multipliers relative to 2π used by extract_parameters. ...
Definition: Gate.cpp:2644
virtual std::vector< int > get_involved_qubits(bool only_target=false)
Call to get the qubits involved in the gate operation.
Definition: Gate.cpp:1219
virtual void reorder_qubits(std::vector< int > qbit_list)
Call to reorder the qubits in the matrix of the operation.
Definition: Gate.cpp:1151
Matrix matrix_alloc
Matrix of the operation.
Definition: Gate.h:119
int get_control_qbit()
Call to get the index of the control qubit.
Definition: Gate.cpp:1211
Class to store data of complex arrays and its properties.
Definition: matrix_real.h:41