Maestro 0.3.11
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
TensorNode.h
Go to the documentation of this file.
1
13
14#pragma once
15
16#ifndef __TENSOR_NODE_H_
17#define __TENSOR_NODE_H_ 1
18
19#include "../Types.h"
21#include "Factory.h"
22#include <Eigen/Eigen>
23#include <memory>
24
25namespace TensorNetworks
26{
27
29{
30 public:
31 using Index = Eigen::Index;
32 using MatrixClass = Eigen::MatrixXcd;
33
34 void SetGate(const QC::Gates::QuantumGateWithOp<MatrixClass> &gate, Types::qubit_t q1, Types::qubit_t q2 = 0)
35 {
36 const size_t qubitsNumber = gate.getQubitsNumber();
37
38 Clear();
39
40 if (qubitsNumber > 2)
41 throw std::invalid_argument("The gate has more than 2 qubits. Decompose gates on a higher number "
42 "of qubits in gates on 1 and 2 qubits only.");
43
44 qubits.push_back(q1);
45
46 if (qubitsNumber > 1)
47 qubits.push_back(q2);
48
49 qubits.push_back(q1);
50 if (qubitsNumber > 1)
51 qubits.push_back(q2);
52
54
55 connections.resize(qubitsNumber * 2, NotConnected);
56 connectionsIndices.resize(qubitsNumber * 2, NotConnected);
57 }
58
59 void SetQubit(Types::qubit_t q, bool zero = true)
60 {
61 Clear();
62
63 qubits.push_back(q);
64
65 if (zero)
67 else
69
70 connections.push_back(NotConnected);
72 }
73
74 void SetProjector(Types::qubit_t q, bool zero = true)
75 {
76 Clear();
77
78 qubits.push_back(q);
79 qubits.push_back(q);
80
82
83 connections.resize(2, NotConnected);
85 }
86
87 void SetSuper()
88 {
89 if (tensor)
90 tensor->Conj();
91 }
92
93 void SetId(Index Id)
94 {
95 id = Id;
96 }
97
98 Index GetId() const
99 {
100 return id;
101 }
102
103 size_t GetQubitsNumber() const
104 {
105 return qubits.size();
106 }
107
108 size_t GetRank() const
109 {
110 if (!tensor)
111 return 0;
112
113 return tensor->GetRank();
114 }
115
116 void Clear()
117 {
118 id = 0;
119
120 tensor.reset();
121 qubits.clear();
122 connections.clear();
123 connectionsIndices.clear();
124 }
125
126 // this is needed for tensor contraction, since connections are modified in
127 // the process
128 std::shared_ptr<TensorNode> CloneWithoutTensorCopy() const
129 {
130 auto cloned = std::make_shared<TensorNode>();
131 cloned->id = id;
132 cloned->qubits = qubits;
133 cloned->connections = connections;
134 cloned->connectionsIndices = connectionsIndices;
135 cloned->tensor = tensor;
136
137 // cloned->contractsTheNeededQubit = contractsTheNeededQubit;
138
139 return cloned;
140 }
141
142 // Useful for contracting the tensor network without actually contracting the
143 // tensors can be used in algorithms that find an (sub)optimal contraction
144 // order
145 std::shared_ptr<TensorNode> CloneWithADummyTensor() const
146 {
147 auto cloned = std::make_shared<TensorNode>();
148 cloned->id = id;
149 cloned->qubits = qubits;
150 cloned->connections = connections;
151 cloned->connectionsIndices = connectionsIndices;
152 cloned->tensor = std::make_shared<Utils::Tensor<>>(tensor->GetDims(), true);
153
154 // cloned->contractsTheNeededQubit = contractsTheNeededQubit;
155
156 return cloned;
157 }
158
159 std::shared_ptr<TensorNode> Clone() const
160 {
161 auto cloned = std::make_shared<TensorNode>();
162 cloned->id = id;
163 cloned->qubits = qubits;
164 cloned->connections = connections;
165 cloned->connectionsIndices = connectionsIndices;
166 cloned->tensor = std::make_shared<Utils::Tensor<>>(*tensor);
167
168 // cloned->contractsTheNeededQubit = contractsTheNeededQubit;
169
170 return cloned;
171 }
172
173 constexpr static Index NotConnected = -1;
174
175 Index id = 0;
176
177 std::vector<Types::qubit_t> qubits; // each dimension/index of the tensor corresponds to a qubit
178 std::vector<Index> connections; // the ids of the connected tensors in the network (equal
179 // with the index in the tensors vector)
180 std::vector<Index> connectionsIndices; // the indices of the connected indices in the
181 // connected tensor on that position
182 std::shared_ptr<Utils::Tensor<>> tensor; // the actual tensor
183
184 // this is only needed for contraction algorithm
185 // it will be set to true only on contraction result nodes, not on the
186 // original tensor nodes
188};
189
190} // namespace TensorNetworks
191
192#endif // __TENSOR_NODE_H_
static std::shared_ptr< Utils::Tensor<> > CreateQubit0Tensor()
static std::shared_ptr< Utils::Tensor<> > CreateProjectionTensor(bool onZero=true)
static std::shared_ptr< Utils::Tensor<> > CreateQubit1Tensor()
static std::shared_ptr< Utils::Tensor<> > CreateTensorFromGate(const QC::Gates::QuantumGateWithOp< MatrixClass > &gate)
std::vector< Index > connectionsIndices
Definition TensorNode.h:180
std::vector< Types::qubit_t > qubits
Definition TensorNode.h:177
std::vector< Index > connections
Definition TensorNode.h:178
std::shared_ptr< TensorNode > CloneWithoutTensorCopy() const
Definition TensorNode.h:128
Eigen::MatrixXcd MatrixClass
Definition TensorNode.h:32
std::shared_ptr< TensorNode > CloneWithADummyTensor() const
Definition TensorNode.h:145
void SetQubit(Types::qubit_t q, bool zero=true)
Definition TensorNode.h:59
size_t GetQubitsNumber() const
Definition TensorNode.h:103
std::shared_ptr< Utils::Tensor<> > tensor
Definition TensorNode.h:182
void SetProjector(Types::qubit_t q, bool zero=true)
Definition TensorNode.h:74
static constexpr Index NotConnected
Definition TensorNode.h:173
std::shared_ptr< TensorNode > Clone() const
Definition TensorNode.h:159
void SetGate(const QC::Gates::QuantumGateWithOp< MatrixClass > &gate, Types::qubit_t q1, Types::qubit_t q2=0)
Definition TensorNode.h:34
uint_fast64_t qubit_t
The type of a qubit.
Definition Types.h:23