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