From 3319d74b1beca7087078c7d99221c53bc6e0425e Mon Sep 17 00:00:00 2001 From: miha-q <> Date: Wed, 14 Aug 2024 20:58:50 -0400 Subject: [PATCH] Wed Aug 14 08:58:50 PM EDT 2024 --- examples/test.txt | 3 +++ src/bytecode.c | 5 +++-- src/bytecode.h | 32 ++++++++++++++++++++++---------- 3 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 examples/test.txt diff --git a/examples/test.txt b/examples/test.txt new file mode 100644 index 0000000..32e85ca --- /dev/null +++ b/examples/test.txt @@ -0,0 +1,3 @@ +qreg q[1]; +ry(pi/2) q[0]; +print q; diff --git a/src/bytecode.c b/src/bytecode.c index e892ccb..9a73886 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -178,10 +178,11 @@ float* qansel_unitary(float theta, float phi, float lambda) c.imaginary = sin(phi) * sin(theta/2.0); d.real = cos(phi + lambda) * cos(theta/2.0); d.imaginary = sin(phi + lambda) * cos(theta/2.0); + //remember it must be transposed!!! cpx_mtx_init(&m, 2, 2); cpx_mtx_set(&m, 0, 0, &a); - cpx_mtx_set(&m, 0, 1, &b); - cpx_mtx_set(&m, 1, 0, &c); + cpx_mtx_set(&m, 0, 1, &c); + cpx_mtx_set(&m, 1, 0, &b); cpx_mtx_set(&m, 1, 1, &d); return m.ptr; } diff --git a/src/bytecode.h b/src/bytecode.h index 50b0ee7..5e97d87 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -28,20 +28,32 @@ typedef struct /* IMPORTANT NOTE - - (AB)^T = (A^T)(B^T) In quantum mechanics, if the relative state is S, then evolving - the relative state by a logic gate A requires computing AS. + the relative state by a logic gate G requires computing GS. The + relative state S is a column vector. + + Traditionally, a one-dimensional array is treated as a single + row. While it could be treated as a single column, I find this + to be unintuitive. Thus, I opt to store the relative state of + the system as a row vector. This means the simulator only ever + stores S^T and not S. + + This means that we cannot apply a logic gate by multiplying G + by the state vector S as it will give the wrong answer. + Rather, we can make use of the equality below. + + (AB)^T = (A^T)(B^T) - This is inefficient as S is a column vector and it is more - efficient in terms of caching to use a row vector. Hence, the - simulator does not store S but only S^T. To apply the logic - gate A, it is not AS that needs to be computed but instead - (S^T)(A^T). + This tells us that we have to instead compute (S^T)(G^T). The + important takeaway here is that all the logic gates represented + here are their transposed equivalents. For most logic gates, + they are equal to their transpose, but the Y gate is a clear + exception here. Y is not equal to Y^T. - This means all the logic gates stored below are represented - as their transposed equivalents. + Keep this in mind if more logic gates are added in the future. + They all must be first transposed before being hardcoded on + this page. */ static float Identity[] = -- 2.39.5