Qrack  9.0
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 + 4, 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 ((norm(gate[1U]) <= FP_NORM_EPSILON) && (norm(gate[2U]) <= FP_NORM_EPSILON)) {
40  gate[1U] = ZERO_R1;
41  gate[2U] = ZERO_R1;
42  gate[0U] /= abs(gate[0U]);
43  gate[3U] /= abs(gate[3U]);
44  }
45  if ((norm(gate[0U]) <= FP_NORM_EPSILON) && (norm(gate[3U]) <= FP_NORM_EPSILON)) {
46  gate[0U] = ZERO_R1;
47  gate[3U] = ZERO_R1;
48  gate[1U] /= abs(gate[1U]);
49  gate[2U] /= abs(gate[2U]);
50  }
51  }
52 
53  bool IsPhase() { return (norm(gate[1U]) <= FP_NORM_EPSILON) && (norm(gate[2U]) <= FP_NORM_EPSILON); }
54 
55  bool IsInvert() { return (norm(gate[0U]) <= FP_NORM_EPSILON) && (norm(gate[3U]) <= FP_NORM_EPSILON); }
56 
57  bool IsHPhase()
58  {
59  return ((norm(gate[0U] - gate[1U]) <= FP_NORM_EPSILON) && (norm(gate[2U] + gate[3U]) <= FP_NORM_EPSILON));
60  }
61 
62  bool IsHInvert()
63  {
64  return ((norm(gate[0U] + gate[1U]) <= FP_NORM_EPSILON) && (norm(gate[2U] - gate[3U]) <= FP_NORM_EPSILON));
65  }
66 
67  bool IsIdentity() { return IsPhase() && (norm(gate[0U] - gate[3U]) <= FP_NORM_EPSILON); }
68 
69  bool IsX(bool randGlobalPhase = true)
70  {
71  return IsInvert() && (norm(gate[1U] - gate[2U]) <= FP_NORM_EPSILON) &&
72  (randGlobalPhase || (norm(ONE_CMPLX - gate[1U]) <= FP_NORM_EPSILON));
73  }
74 
75  bool IsY(bool randGlobalPhase = true)
76  {
77  return IsInvert() && (norm(gate[1U] + gate[2U]) <= FP_NORM_EPSILON) &&
78  (randGlobalPhase || (norm(ONE_CMPLX + gate[1U]) <= FP_NORM_EPSILON));
79  }
80 
81  bool IsZ(bool randGlobalPhase = true)
82  {
83  return IsPhase() && (norm(gate[0U] + gate[3U]) <= FP_NORM_EPSILON) &&
84  (randGlobalPhase || (norm(ONE_CMPLX - gate[0U]) <= FP_NORM_EPSILON));
85  }
86 
87  bool IsH()
88  {
89  return (norm(SQRT1_2_R1 - gate[0U]) <= FP_NORM_EPSILON) && (norm(SQRT1_2_R1 - gate[1U]) <= FP_NORM_EPSILON) &&
91  }
92 };
93 
94 } // namespace Qrack
Definition: complex16x2simd.hpp:25
std::complex< half_float::half > complex
Definition: qrack_types.hpp:62
void mul2x2(complex const *left, complex const *right, complex *out)
Definition: functions.cpp:97
QRACK_CONST real1 FP_NORM_EPSILON
Definition: qrack_types.hpp:243
double norm(const complex2 &c)
Definition: complex16x2simd.hpp:101
QRACK_CONST complex ONE_CMPLX
Definition: qrack_types.hpp:239
const real1 ZERO_R1
Definition: qrack_types.hpp:151
std::shared_ptr< MpsShard > MpsShardPtr
Definition: mpsshard.hpp:18
QRACK_CONST complex ZERO_CMPLX
Definition: qrack_types.hpp:240
const real1 SQRT1_2_R1
Definition: qrack_types.hpp:160
HALF_CONSTEXPR half abs(half arg)
Absolute value.
Definition: half.hpp:2975
MICROSOFT_QUANTUM_DECL void U(_In_ uintq sid, _In_ uintq q, _In_ double theta, _In_ double phi, _In_ double lambda)
(External API) 3-parameter unitary gate
Definition: pinvoke_api.cpp:1362
Definition: mpsshard.hpp:21
bool IsInvert()
Definition: mpsshard.hpp:55
bool IsPhase()
Definition: mpsshard.hpp:53
MpsShard()
Definition: mpsshard.hpp:24
bool IsIdentity()
Definition: mpsshard.hpp:67
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:87
bool IsZ(bool randGlobalPhase=true)
Definition: mpsshard.hpp:81
bool IsX(bool randGlobalPhase=true)
Definition: mpsshard.hpp:69
bool IsHInvert()
Definition: mpsshard.hpp:62
bool IsY(bool randGlobalPhase=true)
Definition: mpsshard.hpp:75
bool IsHPhase()
Definition: mpsshard.hpp:57