Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
CSWAP.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 "CSWAP.h"
24 
25 using namespace std;
26 
31 
32  // A string labeling the gate operation
33  name = "CSWAP";
34 
35  // A string describing the type of the gate
37 
38  // Clear control qubits for nullary constructor
39  control_qbits.clear();
40 }
41 
48 CSWAP::CSWAP(int qbit_num_in, const std::vector<int>& target_qbits_in, const std::vector<int>& control_qbits_in)
49  : SWAP(qbit_num_in, target_qbits_in) {
50 
51  // A string labeling the gate operation
52  name = "CSWAP";
53 
54  // A string describing the type of the gate
56 
57  // Validate that we have exactly 1 control qubit
58  if (control_qbits_in.size() != 1) {
59  std::stringstream sstream;
60  sstream << "CSWAP gate requires exactly 1 control qubit, got " << control_qbits_in.size() << std::endl;
61  print(sstream, 0);
62  throw sstream.str();
63  }
64 
65  // Validate control qubit index
66  if (control_qbits_in[0] >= qbit_num_in) {
67  std::stringstream sstream;
68  sstream << "Control qubit index " << control_qbits_in[0] << " is larger than or equal to the number of qubits" << std::endl;
69  print(sstream, 0);
70  throw sstream.str();
71  }
72 
73  // Check that control qubit doesn't overlap with target qubits
74  for (int tq : target_qbits_in) {
75  if (control_qbits_in[0] == tq) {
76  std::stringstream sstream;
77  sstream << "The control qubit cannot be the same as any target qubit" << std::endl;
78  print(sstream, 0);
79  throw sstream.str();
80  }
81  }
82 
83  // Store control qubit
84  control_qbits = control_qbits_in;
85  control_qbit = control_qbits[0]; // For backward compatibility
86 }
87 
92 }
93 
99 
101 
103  ret->set_parents(parents);
104  ret->set_children(children);
105 
106  return ret;
107 }
std::vector< Gate * > parents
list of parent gates to be applied in the circuit prior to this current gate
Definition: Gate.h:112
Header file for a class representing a CSWAP (Controlled SWAP) operation.
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
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
~CSWAP() override
Destructor of the class.
Definition: CSWAP.cpp:91
void set_children(std::vector< Gate *> &children_)
Call to set the children of the current gate.
Definition: Gate.cpp:2523
A class representing a SWAP operation.
Definition: SWAP.h:35
gate_type type
The type of the operation (see enumeration gate_type)
Definition: Gate.h:96
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
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
virtual CSWAP * clone() override
Call to create a clone of the present class.
Definition: CSWAP.cpp:98
CSWAP()
Nullary constructor of the class.
Definition: CSWAP.cpp:30
std::vector< int > control_qbits
Vector of control qubit indices (for multi-qubit gates)
Definition: Gate.h:104
std::string name
A string labeling the gate operation.
Definition: Gate.h:92
std::vector< Gate * > children
list of child gates to be applied after this current gate
Definition: Gate.h:114
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
A class representing a CSWAP (Controlled SWAP) operation.
Definition: CSWAP.h:35