Qrack  9.13
General classical-emulating-quantum development framework
mpsshard.hpp
Go to the documentation of this file.
1 //
3 // (C) Daniel Strano and the Qrack contributors 2017-2023. All rights reserved.
4 //
5 // This is a multithreaded, universal quantum register simulation, allowing
6 // (nonphysical) register cloning and direct measurement of probability and
7 // phase, to leverage what advantages classical emulation of qubits can have.
8 //
9 // Licensed under the GNU Lesser General Public License V3.
10 // See LICENSE.md in the project root or https://www.gnu.org/licenses/lgpl-3.0.en.html
11 // for details.
12 
13 #pragma once
15 
16 namespace Qrack {
17 
18 struct MpsShard;
19 typedef std::shared_ptr<MpsShard> MpsShardPtr;
20 
21 struct MpsShard {
23 
26  {
27  // Intentionally left blank
28  }
29 
30  MpsShard(const complex* g) { std::copy(g, g + 4U, gate); }
31 
32  MpsShardPtr Clone() { return std::make_shared<MpsShard>(gate); }
33 
34  void Compose(const complex* g)
35  {
36  complex o[4U];
37  std::copy(gate, gate + 4U, o);
38  mul2x2((complex*)g, o, gate);
39  if (IsPhase()) {
40  gate[1U] = ZERO_R1;
41  gate[2U] = ZERO_R1;
42  gate[0U] /= abs(gate[0U]);
43  gate[3U] /= abs(gate[3U]);
44  } else if (IsInvert()) {
45  gate[0U] = ZERO_R1;
46  gate[3U] = ZERO_R1;
47  gate[1U] /= abs(gate[1U]);
48  gate[2U] /= abs(gate[2U]);
49  }
50  }
51 
52  bool IsPhase() { return (norm(gate[1U]) <= FP_NORM_EPSILON) && (norm(gate[2U]) <= FP_NORM_EPSILON); }
53 
54  bool IsInvert() { return (norm(gate[0U]) <= FP_NORM_EPSILON) && (norm(gate[3U]) <= FP_NORM_EPSILON); }
55 
56  bool IsHPhase()
57  {
58  return ((norm(gate[0U] - gate[1U]) <= FP_NORM_EPSILON) && (norm(gate[2U] + gate[3U]) <= FP_NORM_EPSILON));
59  }
60 
61  bool IsHInvert()
62  {
63  return ((norm(gate[0U] + gate[1U]) <= FP_NORM_EPSILON) && (norm(gate[2U] - gate[3U]) <= FP_NORM_EPSILON));
64  }
65 
66  bool IsIdentity() { return IsPhase() && (norm(gate[0U] - gate[3U]) <= FP_NORM_EPSILON); }
67 
68  bool IsX(bool randGlobalPhase = true)
69  {
70  return IsInvert() && (norm(gate[1U] - gate[2U]) <= FP_NORM_EPSILON) &&
71  (randGlobalPhase || (norm(ONE_CMPLX - gate[1U]) <= FP_NORM_EPSILON));
72  }
73 
74  bool IsY(bool randGlobalPhase = true)
75  {
76  return IsInvert() && (norm(gate[1U] + gate[2U]) <= FP_NORM_EPSILON) &&
77  (randGlobalPhase || (norm(ONE_CMPLX + gate[1U]) <= FP_NORM_EPSILON));
78  }
79 
80  bool IsZ(bool randGlobalPhase = true)
81  {
82  return IsPhase() && (norm(gate[0U] + gate[3U]) <= FP_NORM_EPSILON) &&
83  (randGlobalPhase || (norm(ONE_CMPLX - gate[0U]) <= FP_NORM_EPSILON));
84  }
85 
86  bool IsH()
87  {
88  return (norm(SQRT1_2_R1 - gate[0U]) <= FP_NORM_EPSILON) && (norm(SQRT1_2_R1 - gate[1U]) <= FP_NORM_EPSILON) &&
90  }
91 };
92 
93 } // namespace Qrack
GLOSSARY: bitLenInt - "bit-length integer" - unsigned integer ID of qubit position in register bitCap...
Definition: complex16x2simd.hpp:25
QRACK_CONST real1 SQRT1_2_R1
Definition: qrack_types.hpp:180
void mul2x2(const complex *left, const complex *right, complex *out)
Definition: functions.cpp:111
void U(quid sid, bitLenInt q, real1_f theta, real1_f phi, real1_f lambda)
(External API) 3-parameter unitary gate
Definition: wasm_api.cpp:1143
std::complex< real1 > complex
Definition: qrack_types.hpp:128
QRACK_CONST real1 FP_NORM_EPSILON
Definition: qrack_types.hpp:258
double norm(const complex2 &c)
Definition: complex16x2simd.hpp:101
QRACK_CONST complex ONE_CMPLX
Definition: qrack_types.hpp:252
QRACK_CONST real1 ZERO_R1
Definition: qrack_types.hpp:183
std::shared_ptr< MpsShard > MpsShardPtr
Definition: mpsshard.hpp:18
QRACK_CONST complex ZERO_CMPLX
Definition: qrack_types.hpp:253
HALF_CONSTEXPR half abs(half arg)
Absolute value.
Definition: half.hpp:2975
Definition: mpsshard.hpp:21
bool IsInvert()
Definition: mpsshard.hpp:54
bool IsPhase()
Definition: mpsshard.hpp:52
MpsShard()
Definition: mpsshard.hpp:24
bool IsIdentity()
Definition: mpsshard.hpp:66
void Compose(const complex *g)
Definition: mpsshard.hpp:34
MpsShardPtr Clone()
Definition: mpsshard.hpp:32
MpsShard(const complex *g)
Definition: mpsshard.hpp:30
complex gate[4U]
Definition: mpsshard.hpp:22
bool IsH()
Definition: mpsshard.hpp:86
bool IsZ(bool randGlobalPhase=true)
Definition: mpsshard.hpp:80
bool IsX(bool randGlobalPhase=true)
Definition: mpsshard.hpp:68
bool IsHInvert()
Definition: mpsshard.hpp:61
bool IsY(bool randGlobalPhase=true)
Definition: mpsshard.hpp:74
bool IsHPhase()
Definition: mpsshard.hpp:56