Maestro 0.1.0
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
TensorNode.h
Go to the documentation of this file.
1
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
28 public:
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) qubits.push_back(q2);
46
47 qubits.push_back(q1);
48 if (qubitsNumber > 1) qubits.push_back(q2);
49
51
52 connections.resize(qubitsNumber * 2, NotConnected);
53 connectionsIndices.resize(qubitsNumber * 2, NotConnected);
54 }
55
56 void SetQubit(Types::qubit_t q, bool zero = true) {
57 Clear();
58
59 qubits.push_back(q);
60
61 if (zero)
63 else
65
66 connections.push_back(NotConnected);
68 }
69
70 void SetProjector(Types::qubit_t q, bool zero = true) {
71 Clear();
72
73 qubits.push_back(q);
74 qubits.push_back(q);
75
77
78 connections.resize(2, NotConnected);
80 }
81
82 void SetSuper() {
83 if (tensor) tensor->Conj();
84 }
85
86 void SetId(Index Id) { id = Id; }
87
88 Index GetId() const { return id; }
89
90 size_t GetQubitsNumber() const { return qubits.size(); }
91
92 size_t GetRank() const {
93 if (!tensor) return 0;
94
95 return tensor->GetRank();
96 }
97
98 void Clear() {
99 id = 0;
100
101 tensor.reset();
102 qubits.clear();
103 connections.clear();
104 connectionsIndices.clear();
105 }
106
107 // this is needed for tensor contraction, since connections are modified in
108 // the process
109 std::shared_ptr<TensorNode> CloneWithoutTensorCopy() const {
110 auto cloned = std::make_shared<TensorNode>();
111 cloned->id = id;
112 cloned->qubits = qubits;
113 cloned->connections = connections;
114 cloned->connectionsIndices = connectionsIndices;
115 cloned->tensor = tensor;
116
117 // cloned->contractsTheNeededQubit = contractsTheNeededQubit;
118
119 return cloned;
120 }
121
122 // Useful for contracting the tensor network without actually contracting the
123 // tensors can be used in algorithms that find an (sub)optimal contraction
124 // order
125 std::shared_ptr<TensorNode> CloneWithADummyTensor() const {
126 auto cloned = std::make_shared<TensorNode>();
127 cloned->id = id;
128 cloned->qubits = qubits;
129 cloned->connections = connections;
130 cloned->connectionsIndices = connectionsIndices;
131 cloned->tensor = std::make_shared<Utils::Tensor<>>(tensor->GetDims(), true);
132
133 // cloned->contractsTheNeededQubit = contractsTheNeededQubit;
134
135 return cloned;
136 }
137
138 std::shared_ptr<TensorNode> Clone() const {
139 auto cloned = std::make_shared<TensorNode>();
140 cloned->id = id;
141 cloned->qubits = qubits;
142 cloned->connections = connections;
143 cloned->connectionsIndices = connectionsIndices;
144 cloned->tensor = std::make_shared<Utils::Tensor<>>(*tensor);
145
146 // cloned->contractsTheNeededQubit = contractsTheNeededQubit;
147
148 return cloned;
149 }
150
151 constexpr static Index NotConnected = -1;
152
153 Index id = 0;
154
155 std::vector<Types::qubit_t>
156 qubits; // each dimension/index of the tensor corresponds to a qubit
157 std::vector<Index>
158 connections; // the ids of the connected tensors in the network (equal
159 // with the index in the tensors vector)
160 std::vector<Index>
161 connectionsIndices; // the indices of the connected indices in the
162 // connected tensor on that position
163 std::shared_ptr<Utils::Tensor<>> tensor; // the actual tensor
164
165 // this is only needed for contraction algorithm
166 // it will be set to true only on contraction result nodes, not on the
167 // original tensor nodes
169};
170
171} // namespace TensorNetworks
172
173#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:161
std::vector< Types::qubit_t > qubits
Definition TensorNode.h:156
std::vector< Index > connections
Definition TensorNode.h:158
std::shared_ptr< TensorNode > CloneWithoutTensorCopy() const
Definition TensorNode.h:109
Eigen::MatrixXcd MatrixClass
Definition TensorNode.h:30
std::shared_ptr< TensorNode > CloneWithADummyTensor() const
Definition TensorNode.h:125
void SetQubit(Types::qubit_t q, bool zero=true)
Definition TensorNode.h:56
size_t GetQubitsNumber() const
Definition TensorNode.h:90
std::shared_ptr< Utils::Tensor<> > tensor
Definition TensorNode.h:163
void SetProjector(Types::qubit_t q, bool zero=true)
Definition TensorNode.h:70
static constexpr Index NotConnected
Definition TensorNode.h:151
std::shared_ptr< TensorNode > Clone() const
Definition TensorNode.h:138
void SetGate(const QC::Gates::QuantumGateWithOp< MatrixClass > &gate, Types::qubit_t q1, Types::qubit_t q2=0)
Definition TensorNode.h:32