-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatedata.cpp
More file actions
163 lines (151 loc) · 4.03 KB
/
gatedata.cpp
File metadata and controls
163 lines (151 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Programmer : Noah Klein
// Instructor : Price
// Class : CS5201 Spring 2020
// Assignment : Homework 6 - Complex Numbers, Outer Products, and Quantum
// Computing
// Filename : gatedata.cpp
#include "gatedata.h"
//***************************** Essentials **********************************//
gatedata::gatedata()
{
m_Pnot = new nTrix<cpf>({{cpf(1,0),cpf(0,0)},{cpf(0,0),cpf(0,0)}});
m_Pone = new nTrix<cpf>({{cpf(0,0),cpf(0,0)},{cpf(0,0),cpf(1,0)}});
m_identity = new nTrix<cpf>({{cpf(1,0),cpf(0,0)},{cpf(0,0),cpf(1,0)}});
m_gate = NULL;
}
gatedata::gatedata(const nTrix<cpf> base)
{
if(base.rows() != 2 || base.cols() != 2)
{
std::cout << "Invalid size of matrix for gate operation: ";
throw(std::domain_error(std::to_string(base.rows())));
}
m_Pnot = new nTrix<cpf>({{cpf(1,0),cpf(0,0)},{cpf(0,0),cpf(0,0)}});
m_Pone = new nTrix<cpf>({{cpf(0,0),cpf(0,0)},{cpf(0,0),cpf(1,0)}});
m_identity = new nTrix<cpf>({{cpf(1,0),cpf(0,0)},{cpf(0,0),cpf(1,0)}});
m_gate = new nTrix<cpf>(base);
}
gatedata::~gatedata()
{
delete m_Pnot;
delete m_Pone;
delete m_identity;
delete m_gate;
}
gatedata::gatedata(const gatedata& rhs)
{
m_Pnot = new nTrix<cpf>(*rhs.m_Pnot);
m_Pone = new nTrix<cpf>(*rhs.m_Pone);
m_identity = new nTrix<cpf>(*rhs.m_identity);
m_gate = new nTrix<cpf>(*rhs.m_gate);
}
gatedata& gatedata::operator=(const gatedata& rhs)
{
if(m_Pnot)
{
delete m_Pnot;
m_Pnot = NULL;
}
if(m_Pone)
{
delete m_Pone;
m_Pone = NULL;
}
if(m_identity)
{
delete m_identity;
m_identity = NULL;
}
if(m_gate)
{
delete m_gate;
m_gate = NULL;
}
m_Pnot = new nTrix<cpf>(*rhs.m_Pnot);
m_Pone = new nTrix<cpf>(*rhs.m_Pone);
m_identity = new nTrix<cpf>(*rhs.m_identity);
m_gate = new nTrix<cpf>(*rhs.m_gate);
}
//***************************** Mutators ************************************//
nTrix<cpf> gatedata::creation(const listy& a, const listy& b, const int size)
const
{
if(((a.size() + b.size()) > (unsigned int)size) || a.size() < 1)
{
std::cout << "Incorrect amount of control/applied bits: ";
throw(std::domain_error(std::to_string(a.size())));
}
for(int i = 0; i < size; ++i)
{
if((std::count(a.begin(),a.end(),i) + std::count(b.begin(),b.end(),i)) > 1)
{
std::cout << "Cannot apply a gate to a control bit: ";
throw(std::domain_error(std::to_string(i)));
}
}
for(auto itr = a.begin(); itr != a.end(); itr++)
{
if(*itr >= size || *itr < 0)
{
std::cout << "Qubit out of range: ";
throw(std::range_error(std::to_string(*itr)));
}
}
for(auto itr = b.begin(); itr != b.end(); itr++)
{
if(*itr >= size || *itr < 0)
{
std::cout << "Control qubit out of range: ";
throw(std::range_error(std::to_string(*itr)));
}
}
nTrix<cpf> temp;
if(b.size())
{
nVect<nTrix<cpf>> u_not;
nVect<nTrix<cpf>> u_one;
for(int i = 0; i < size; ++i)
{
if(std::count(a.begin(),a.end(),i)) //if that qubit is supposed to have
//the gate applied
{
u_not.push_front(*m_identity);
u_one.push_front(*m_gate);
}
else if(std::count(b.begin(),b.end(),i)) //if that qubit is supposed to
//have the control gate applied
{
u_not.push_front(*m_Pnot);
u_one.push_front(*m_Pone);
}
else //if that qubit is not multiplied by a gate and is not a control
//qubit
{
u_not.push_front(*m_identity);
u_one.push_front(*m_identity);
}
}
temp = (std::accumulate(u_not.begin(),
u_not.end(),nTrix<cpf>({{cpf(1,0)}}),m_prod) +
std::accumulate(u_one.begin(),u_one.end()
,nTrix<cpf>({{cpf(1,0)}}),m_prod));
}
else
{
nVect<nTrix<cpf>> holder;
for(int i = 0; i < size; ++i)
{
if(std::count(a.begin(),a.end(),i))
{
holder.push_front(*m_gate);
}
else
{
holder.push_front(*m_identity);
}
}
temp = (std::accumulate(holder.begin(),
holder.end(),nTrix<cpf>({{cpf(1,0)}}),m_prod));
}
return temp;
}