Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
RYY.cpp
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 #include "RYY.h"
24 #include "gate_kernel_templates.h"
25 using namespace std;
26 
30 RYY::RYY() : Gate() {
31 
32  // A string labeling the gate operation
33  name = "RYY";
34 
35  // A string describing the type of the gate
37 
38  // Initialize target qubits vector (empty for nullary constructor)
39  target_qbits.clear();
40 
41  parameter_num = 1;
42 }
43 
49 RYY::RYY(int qbit_num_in, const std::vector<int>& target_qbits_in)
50  : Gate(qbit_num_in, target_qbits_in) {
51 
52  // A string labeling the gate operation
53  name = "RYY";
54 
55  // A string describing the type of the gate
57 
58  // Validate that we have exactly 2 target qubits
59  if (target_qbits_in.size() != 2) {
60  std::stringstream sstream;
61  sstream << "RYY gate requires exactly 2 target qubits, got " << target_qbits_in.size() << std::endl;
62  print(sstream, 0);
63  throw sstream.str();
64  }
65 
66  // Check that target qubits are unique
67  if (target_qbits_in[0] == target_qbits_in[1]) {
68  std::stringstream sstream;
69  sstream << "The two target qubits cannot be the same" << std::endl;
70  print(sstream, 0);
71  throw sstream.str();
72  }
73 
74  parameter_num = 1;
75 }
76 
81 
82 }
83 
84 Matrix RYY::gate_kernel(const Matrix_real& precomputed_sincos) {
85  Matrix ret;
86  gate_kernel_to(precomputed_sincos, ret);
87  return ret;
88 }
89 
90 Matrix_float RYY::gate_kernel(const Matrix_real_float& precomputed_sincos) {
91  Matrix_float ret;
92  gate_kernel_to(precomputed_sincos, ret);
93  return ret;
94 }
95 
96 Matrix RYY::inverse_gate_kernel(const Matrix_real& precomputed_sincos) {
97  Matrix ret;
98  inverse_gate_kernel_to(precomputed_sincos, ret);
99  return ret;
100 }
101 
103  Matrix_float ret;
104  inverse_gate_kernel_to(precomputed_sincos, ret);
105  return ret;
106 }
107 
108 Matrix RYY::derivative_kernel(const Matrix_real& precomputed_sincos, int param_idx) {
109  Matrix ret;
110  derivative_kernel_to(precomputed_sincos, param_idx, ret);
111  return ret;
112 }
113 
114 Matrix_float RYY::derivative_kernel(const Matrix_real_float& precomputed_sincos, int param_idx) {
115  Matrix_float ret;
116  derivative_kernel_to(precomputed_sincos, param_idx, ret);
117  return ret;
118 }
119 
120 void RYY::gate_kernel_to(const Matrix_real& precomputed_sincos, Matrix& output) {
121  const int theta_offset = 0 * precomputed_sincos.stride;
122  const double s_theta = precomputed_sincos[theta_offset + 0];
123  const double c_theta = precomputed_sincos[theta_offset + 1];
124  build_ryy_kernel_from_trig_to<Matrix, double>(output, s_theta, c_theta);
125 }
126 
127 void RYY::gate_kernel_to(const Matrix_real_float& precomputed_sincos, Matrix_float& output) {
128  const int theta_offset = 0 * precomputed_sincos.stride;
129  const float s_theta = precomputed_sincos[theta_offset + 0];
130  const float c_theta = precomputed_sincos[theta_offset + 1];
131  build_ryy_kernel_from_trig_to<Matrix_float, float>(output, s_theta, c_theta);
132 }
133 
134 void RYY::inverse_gate_kernel_to(const Matrix_real& precomputed_sincos, Matrix& output) {
135  const int theta_offset = 0 * precomputed_sincos.stride;
136  const double s_theta = precomputed_sincos[theta_offset + 0];
137  const double c_theta = precomputed_sincos[theta_offset + 1];
138  build_ryy_kernel_from_trig_to<Matrix, double>(output, -s_theta, c_theta);
139 }
140 
142  const int theta_offset = 0 * precomputed_sincos.stride;
143  const float s_theta = precomputed_sincos[theta_offset + 0];
144  const float c_theta = precomputed_sincos[theta_offset + 1];
145  build_ryy_kernel_from_trig_to<Matrix_float, float>(output, -s_theta, c_theta);
146 }
147 
148 void RYY::derivative_kernel_to(const Matrix_real& precomputed_sincos, int param_idx, Matrix& output) {
149  if (param_idx != 0) {
150  output = Matrix();
151  return;
152  }
153  const int theta_offset = 0 * precomputed_sincos.stride;
154  const double s_theta = precomputed_sincos[theta_offset + 0];
155  const double c_theta = precomputed_sincos[theta_offset + 1];
156  build_ryy_derivative_kernel_from_trig_to<Matrix, double>(output, s_theta, c_theta);
157 }
158 
159 void RYY::derivative_kernel_to(const Matrix_real_float& precomputed_sincos, int param_idx, Matrix_float& output) {
160  if (param_idx != 0) {
161  output = Matrix_float();
162  return;
163  }
164  const int theta_offset = 0 * precomputed_sincos.stride;
165  const float s_theta = precomputed_sincos[theta_offset + 0];
166  const float c_theta = precomputed_sincos[theta_offset + 1];
167  build_ryy_derivative_kernel_from_trig_to<Matrix_float, float>(output, s_theta, c_theta);
168 }
169 
170 
176 
177  RYY* ret = new RYY(qbit_num, target_qbits);
178 
180  ret->set_parents(parents);
181  ret->set_children(children);
182 
183  return ret;
184 }
185 
186 std::vector<double>
188  return {2.0};
189 }
std::vector< Gate * > parents
list of parent gates to be applied in the circuit prior to this current gate
Definition: Gate.h:112
Gate()
Default constructor of the class.
Definition: Gate.cpp:121
void inverse_gate_kernel_to(const Matrix_real &precomputed_sincos, Matrix &output) override
Definition: RYY.cpp:134
RYY()
Nullary constructor of the class.
Definition: RYY.cpp:30
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
Class to store single-precision real arrays and properties.
Matrix inverse_gate_kernel(const Matrix_real &precomputed_sincos) override
Definition: RYY.cpp:96
std::vector< int > target_qbits
Vector of target qubit indices (for multi-qubit gates)
Definition: Gate.h:102
int stride
The column stride of the array. (The array elements in one row are a_0, a_1, ... a_{cols-1}, 0, 0, 0, 0. The number of zeros is stride-cols)
Definition: matrix_base.hpp:46
void set_children(std::vector< Gate *> &children_)
Call to set the children of the current gate.
Definition: Gate.cpp:2523
A class representing a RYY gate.
Definition: RYY.h:34
void derivative_kernel_to(const Matrix_real &precomputed_sincos, int param_idx, Matrix &output) override
Definition: RYY.cpp:148
gate_type type
The type of the operation (see enumeration gate_type)
Definition: Gate.h:96
std::vector< double > get_parameter_multipliers() const override
Call to extract parameters from the parameter array corresponding to the circuit, in which the gate i...
Definition: RYY.cpp:187
void gate_kernel_to(const Matrix_real &precomputed_sincos, Matrix &output) override
Definition: RYY.cpp:120
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
Matrix gate_kernel(const Matrix_real &precomputed_sincos) override
Build and return the 4x4 RYY unitary for the given parameter.
Definition: RYY.cpp:84
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
Matrix derivative_kernel(const Matrix_real &precomputed_sincos, int param_idx) override
Definition: RYY.cpp:108
Double-precision complex matrix (float64).
Definition: matrix.h:38
~RYY()
Destructor of the class.
Definition: RYY.cpp:80
std::string name
A string labeling the gate operation.
Definition: Gate.h:92
Single-precision complex matrix (float32).
Definition: matrix_float.h:41
RYY * clone() override
Call to create a clone of the present class.
Definition: RYY.cpp:175
std::vector< Gate * > children
list of child gates to be applied after this current gate
Definition: Gate.h:114
int parameter_num
the number of free parameters of the operation
Definition: Gate.h:108
void set_parents(std::vector< Gate *> &parents_)
Call to set the parents of the current gate.
Definition: Gate.cpp:2511
int qbit_num
number of qubits spanning the matrix of the operation
Definition: Gate.h:94
Class to store data of complex arrays and its properties.
Definition: matrix_real.h:41
Class representing a RYY gate.