Repository navigation
Expand file tree
/
Copy pathgraphsim.h
More file actions
210 lines (171 loc) · 6.63 KB
/
Copy pathgraphsim.h
File metadata and controls
210 lines (171 loc) · 6.63 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// graphsim.h
/*!\mainpage
This is 'graphsim', a simulator for stabilizer quantum cuircuits using the
graph state formalism.
graphsim version 0.10, dated 2005-Jan-27
(c) Simon Anders (sanders@fs.tum.de), University of Innsbruck
See the article
S. Anders, H. J. Briegel:
Fast simulation of Stabilizer Circuits using a Graph States Formalism
quant-ph/0504117
for a description of this software.
----
// This file is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this file; see the file COPYING. If not, browse to
// http://www.fsf.org/licenses/gpl.html
*/
/*!\file
This header file defines the main interface of graphsim.
(c) Simon Anders, University of Innsbruck, 2005
released under GPL.
Note: If you have trouble compiling this, please note:
This file uses the "hash_set" template, which is an extension
to the Standard C++ Library and the Standard Template Library,
specified by SGI. Most C++ compilers have this template.
For GNU C++, the header file <ext/hash_set> hasa to be included
and hash_set has to be prefixed with namespace __gnu_cxx.
If you use another compiler, you might have to change the include
file and the namespace identifier.
*/
#ifndef GRAPHSIM_H
#define GRAPHSIM_H
//The following directives are for SWIG, see http://www.swig.org
#ifdef SWIG
%module (docstring="Graph State Stabilizer Simulator -- S. Anders") graphsim
%{
#include "graphsim.h"
#include "loccliff.h"
#include "stabilizer.h"
%}
%include "cpointer.i"
%pointer_class (bool, boolpc);
%feature ("autodoc", "1");
%rename (print_tbl) Stabilizer::print;
%rename (print_kets) CBDecomposition::print;
%include "graphsim.h"
%include "loccliff.h"
%include "stabilizer.h"
#endif // SWIG
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cassert>
#include "loccliff.h"
#include "stabilizer.h"
#include <cstring>
#include <cstdlib>
using namespace std;
/*! All vertices in a graph state are numbered beginning with 0. To specify
auch an index, the type VertexIndex (which is just unsigned long) is always
used */
typedef unsigned long VertexIndex;
/*! A GraphRegister object maintains a list of its vertices (qubits), each
described by an object of the class QubitVertex described here.*/
struct QubitVertex {
/*!byprod is the vertex operator (VOp) associated with the qubit (the name
stems from the term 'byproduct operator' used for the similar concept in
the one-way quantum computer.*/
LocCliffOp byprod;
/*! neigbors is the adjacency list for this vertex */
hash_set<VertexIndex> neighbors;
/*! Upon construction, a qubit vertex is initialised with the Hadamard
operation as VOp, and with wmpty neighbor list. This makes it represent
a |0>. */
QubitVertex (void)
: byprod (lco_H) {};
};
#ifndef SWIG
/*! This structure is only for internal use for the cphase functions. */
struct ConnectionInfo {
bool wasEdge;
bool non1;
bool non2;
};
#endif
//! A quantum register.
/*! GraphRegister is the central class of graphsim. It represents a register of qubits
that can be entangled with each other. It offers functions to initialize the register,
let gates operate on the qubits, do measurements and print out the state. */
class GraphRegister {
public:
/*! This vector stores all the qubits, represented as QubitVertex objects. The index
of the vector is usually taken as of type VertexIndex. */
vector<QubitVertex> vertices;
GraphRegister (VertexIndex numQubits, int randomize = -1);
GraphRegister (GraphRegister& gr);
~GraphRegister () {};
void local_op (VertexIndex v, LocCliffOp o);
void hadamard (VertexIndex v);
void phaserot (VertexIndex v);
void bitflip (VertexIndex v);
void phaseflip (VertexIndex v);
void cphase (VertexIndex v1, VertexIndex v2);
void cnot (VertexIndex vc, VertexIndex vt);
int measure (VertexIndex v, LocCliffOp basis = lco_Z,
bool* determined = NULL, int force = -1);
Stabilizer & get_full_stabilizer (void) const;
void invert_neighborhood (VertexIndex v);
void print_adj_list (ostream& os = cout) const;
void print_adj_list_line (ostream& os, VertexIndex i) const;
void print_stabilizer (ostream& os = cout) const;
private:
void add_edge (VertexIndex v1, VertexIndex v2);
void del_edge (VertexIndex v1, VertexIndex v2);
void toggle_edge (VertexIndex v1, VertexIndex v2);
int graph_Z_measure (VertexIndex v, int force = -1);
int graph_Y_measure (VertexIndex v, int force = -1);
int graph_X_measure (VertexIndex v, bool* determined = NULL, int force = -1);
void toggle_edges (const hash_set<VertexIndex> vs1,
const hash_set<VertexIndex> vs2);
bool remove_byprod_op (VertexIndex v, VertexIndex use_not);
void cphase_with_table (VertexIndex v1, VertexIndex v2);
ConnectionInfo getConnectionInfo (VertexIndex v1, VertexIndex v2);
};
#ifndef SWIG
/*! As we often iterate over sublists of GraphRegister::vertices, this
iterator typedef is a handy abbreviation. */
typedef vector<QubitVertex>::iterator VertexIter;
/*! Another iterator, this one for the adjacency lists QubitVertex::neigbors,
and subsets. */
typedef hash_set<VertexIndex>::iterator VtxIdxIter;
/*! A constant version of VertexIter */
typedef vector<QubitVertex>::const_iterator VertexIterConst;
/*! A constant version of VtxIdxIter */
typedef hash_set<VertexIndex>::const_iterator VtxIdxIterConst;
/*! Apply the local (i.e. single-qubit) operation o on vertex v. */
inline void GraphRegister::local_op (VertexIndex v, LocCliffOp o) {
vertices[v].byprod = o * vertices[v].byprod;
}
/*! Apply a Hadamard gate on vertex v */
inline void GraphRegister::hadamard (VertexIndex v) {
local_op (v, lco_H);
}
/*! Apply a phaserot gate on vertex v. Phaserot means the gate
S = |0><0| + i |1><1|. */
inline void GraphRegister::phaserot (VertexIndex v) {
local_op (v, lco_S);
}
/*! Apply a bitflip gate (i.e. a Pauli X) on vertex v */
inline void GraphRegister::bitflip (VertexIndex v) {
local_op (v, lco_X);
}
/*! Apply a phaseflip gate (i.e. a Pauli Z) on vertex v */
inline void GraphRegister::phaseflip (VertexIndex v) {
local_op (v, lco_Z);
}
#endif //SWIG
//#define DEBUGOUTPUT
#ifdef DEBUGOUTPUT
#define DBGOUT(a) cout << a
#else
#define DBGOUT(a)
#endif
#endif //GRAPHSIM_H