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