Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
CCX.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 "CCX.h"
24 
25 
26 using namespace std;
27 
28 
32 CCX::CCX() : Gate(){
33 
34  // A string labeling the gate operation
35  name = "CCX";
36 
37  // A string describing the type of the gate
39 
40  // Initialize control qubits vector (empty for nullary constructor)
41  control_qbits.clear();
42 }
43 
44 
51 CCX::CCX(int qbit_num_in, int target_qbit_in, const std::vector<int>& control_qbits_in)
52  : Gate(qbit_num_in, std::vector<int>{target_qbit_in}, control_qbits_in) {
53 
54  // A string labeling the gate operation
55  name = "CCX";
56 
57  // A string describing the type of the gate
59 
60  // Validate that we have exactly 2 control qubits
61  if (control_qbits_in.size() != 2) {
62  std::stringstream sstream;
63  sstream << "CCX gate requires exactly 2 control qubits, got " << control_qbits_in.size() << std::endl;
64  print(sstream, 0);
65  throw sstream.str();
66  }
67 
68  // Check that control qubits are unique
69  if (control_qbits_in[0] == control_qbits_in[1]) {
70  std::stringstream sstream;
71  sstream << "The two control qubits cannot be the same" << std::endl;
72  print(sstream, 0);
73  throw sstream.str();
74  }
75 }
76 
81 }
82 
88 
89  CCX* ret = new CCX( qbit_num, target_qbit, control_qbits );
90 
92  ret->set_parents( parents );
93  ret->set_children( children );
94 
95  return ret;
96 
97 }
98 
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 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
A class representing a CCX (Toffoli) operation.
Definition: CCX.h:35
~CCX() override
Destructor of the class.
Definition: CCX.cpp:80
void set_children(std::vector< Gate *> &children_)
Call to set the children of the current gate.
Definition: Gate.cpp:2523
int target_qbit
The index of the qubit on which the operation acts (target_qbit >= 0)
Definition: Gate.h:98
Header file for a class representing a CCX (Toffoli) operation.
CCX()
Nullary constructor of the class.
Definition: CCX.cpp:32
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
std::vector< int > control_qbits
Vector of control qubit indices (for multi-qubit gates)
Definition: Gate.h:104
virtual CCX * clone() override
Call to create a clone of the present class.
Definition: CCX.cpp:87
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