Maestro 0.3.11
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
TensorNetworks/Factory.h
Go to the documentation of this file.
1
11
12#pragma once
13
14#ifndef __TENSORS_FACTORY_H_
15#define __TENSORS_FACTORY_H_ 1
16
18#include "QubitRegister.h"
19
20namespace TensorNetworks
21{
22
29{
30 public:
31 using MatrixClass = Eigen::MatrixXcd;
32
33 static std::shared_ptr<Utils::Tensor<>> CreateTensorFromGate(const QC::Gates::QuantumGateWithOp<MatrixClass> &gate)
34 {
35 if (gate.getQubitsNumber() > 2)
36 throw std::invalid_argument("The gate has more than 2 qubits. Decompose gates on a higher number "
37 "of qubits in gates on 1 and 2 qubits only.");
38
39 if (gate.getQubitsNumber() == 1)
41
43 }
44
45 static std::shared_ptr<Utils::Tensor<>> CreateProjectionTensor(bool onZero = true)
46 {
47 static const std::vector<size_t> dims{2, 2};
48 auto tensor = std::make_shared<Utils::Tensor<>>(dims);
49
50 std::vector<size_t> indices{0, 0};
51 (*tensor)(indices) = onZero ? 1. : 0.;
52 indices[1] = 1;
53 (*tensor)(indices) = 0;
54 indices[0] = 1;
55 (*tensor)(indices) = onZero ? 0. : 1.;
56 indices[1] = 0;
57 (*tensor)(indices) = 0;
58
59 return tensor;
60 }
61
62 static std::shared_ptr<Utils::Tensor<>> CreateQubit0Tensor()
63 {
64 static const std::vector<size_t> dims{2};
65 auto tensor = std::make_shared<Utils::Tensor<>>(dims);
66
67 std::vector<size_t> indices{0};
68 (*tensor)(indices) = 1.0;
69 indices[0] = 1;
70 (*tensor)(indices) = 0.0;
71
72 return tensor;
73 }
74
75 static std::shared_ptr<Utils::Tensor<>> CreateQubit1Tensor()
76 {
77 static const std::vector<size_t> dims{2};
78 auto tensor = std::make_shared<Utils::Tensor<>>(dims);
79
80 std::vector<size_t> indices{0};
81 (*tensor)(indices) = 0.0;
82 indices[0] = 1;
83 (*tensor)(indices) = 1.0;
84
85 return tensor;
86 }
87
88 static std::shared_ptr<Utils::Tensor<>> CreateTensorFromGateOnOneQubit(const QC::Gates::QuantumGateWithOp<MatrixClass> &gate)
89 {
90 static const std::vector<size_t> dims{2, 2};
91 auto tensor = std::make_shared<Utils::Tensor<>>(dims);
92
93 const MatrixClass &mat = gate.getRawOperatorMatrix();
94
95 std::vector<size_t> indices{0, 0};
96 (*tensor)(indices) = mat(0, 0);
97 indices[1] = 1;
98 (*tensor)(indices) = mat(1, 0);
99 indices[0] = 1;
100 (*tensor)(indices) = mat(1, 1);
101 indices[1] = 0;
102 (*tensor)(indices) = mat(0, 1);
103
104 return tensor;
105 }
106
107 static std::shared_ptr<Utils::Tensor<>> CreateTensorFromGateOnTwoQubits(const QC::Gates::QuantumGateWithOp<MatrixClass> &gate)
108 {
109 static const std::vector<size_t> dims{2, 2, 2, 2};
110 auto tensor = std::make_shared<Utils::Tensor<>>(dims);
111
112 const MatrixClass &mat = gate.getRawOperatorMatrix();
113
114 std::vector<size_t> indices;
115 indices.resize(4);
116
117 // q1l, q0l, q1c, q0c
118 for (int q0l = 0; q0l < 2; ++q0l) // ctrl qubit
119 {
120 const int l0 = q0l << 1;
121 indices[2] = q0l;
122 for (int q0c = 0; q0c < 2; ++q0c) // ctrl qubit
123 {
124 indices[0] = q0c;
125 const int c0 = q0c << 1;
126 for (int q1l = 0; q1l < 2; ++q1l)
127 {
128 indices[3] = q1l;
129 for (int q1c = 0; q1c < 2; ++q1c)
130 {
131 indices[1] = q1c;
132 (*tensor)(indices) = mat(l0 | q1l, c0 | q1c);
133 }
134 }
135 }
136 }
137
138 return tensor;
139 }
140};
141
142} // namespace TensorNetworks
143
144#endif // ! __TENSORS_FACTORY_H_
The factory for gates tensors.
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)
static std::shared_ptr< Utils::Tensor<> > CreateTensorFromGateOnTwoQubits(const QC::Gates::QuantumGateWithOp< MatrixClass > &gate)
static std::shared_ptr< Utils::Tensor<> > CreateTensorFromGateOnOneQubit(const QC::Gates::QuantumGateWithOp< MatrixClass > &gate)