aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-08-29 03:55:18 +0200
committerhachem <im@hachem.wtf>2026-08-29 03:55:18 +0200
commit94612a0be0e691d68bcd27a33e529ad30fa3df43 (patch)
tree8d93b8d03ffa3170e07c10cd86b2366c9ca7a30c
parenta159f48900f1927b05d4b8f3d04459610eb9897e (diff)
add: complex numbers
-rw-r--r--examples/tester/main.c9
-rw-r--r--premake5.lua6
-rw-r--r--src/maths/complex.c142
-rw-r--r--src/maths/complex.h28
-rw-r--r--tester/benchmarks.rs (renamed from examples/tester/benchmarks.rs)0
-rw-r--r--tester/clifford.rs (renamed from examples/tester/clifford.rs)0
-rw-r--r--tester/common.rs (renamed from examples/tester/common.rs)0
-rw-r--r--tester/custom_gates.rs (renamed from examples/tester/custom_gates.rs)0
-rw-r--r--tester/kernels.rs (renamed from examples/tester/kernels.rs)0
-rw-r--r--tester/main.c23
-rw-r--r--tester/main.rs (renamed from examples/tester/main.rs)0
-rw-r--r--tester/noise.rs (renamed from examples/tester/noise.rs)0
-rw-r--r--tester/non_clifford.rs (renamed from examples/tester/non_clifford.rs)0
-rw-r--r--tester/simd.rs (renamed from examples/tester/simd.rs)0
14 files changed, 196 insertions, 12 deletions
diff --git a/examples/tester/main.c b/examples/tester/main.c
deleted file mode 100644
index 9a9428f..0000000
--- a/examples/tester/main.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <stdio.h>
-
-#include "psi.h"
-
-int main(void)
-{
- printf("psi %s\n", psi_version());
- return 0;
-}
diff --git a/premake5.lua b/premake5.lua
index 8aa81ca..8038a9a 100644
--- a/premake5.lua
+++ b/premake5.lua
@@ -98,13 +98,13 @@ project "tester"
setup_c_target()
files {
- "examples/tester/**.h",
- "examples/tester/**.c",
+ "tester/**.h",
+ "tester/**.c",
}
includedirs {
"src",
- "examples/tester",
+ "tester",
}
links "psi"
diff --git a/src/maths/complex.c b/src/maths/complex.c
new file mode 100644
index 0000000..3115fdd
--- /dev/null
+++ b/src/maths/complex.c
@@ -0,0 +1,142 @@
+#include "maths/complex.h"
+
+#include <math.h>
+
+struct PsiComplex psi_new_complex(double real, double imaginary)
+{
+ return (struct PsiComplex)
+ {
+ real,
+ imaginary,
+ };
+}
+
+struct PsiComplex psi_new_complex_from_real(double real)
+{
+ return (struct PsiComplex)
+ {
+ real,
+ 0.0,
+ };
+}
+
+struct PsiComplex psi_conjugate_complex(struct PsiComplex z)
+{
+ return (struct PsiComplex)
+ {
+ z.real,
+ -z.imaginary,
+ };
+}
+
+struct PsiComplex psi_neg_complex(struct PsiComplex z)
+{
+ return (struct PsiComplex)
+ {
+ -z.real,
+ -z.imaginary,
+ };
+}
+
+double psi_phase_complex(struct PsiComplex z)
+{
+ return atan2(z.imaginary, z.real);
+}
+
+double psi_norm2_complex(struct PsiComplex z)
+{
+ return z.real * z.real + z.imaginary * z.imaginary;
+}
+
+double psi_abs_complex(struct PsiComplex z)
+{
+ return sqrt(psi_norm2_complex(z));
+}
+
+struct PsiComplex psi_sqrt_complex(struct PsiComplex z)
+{
+ double r = psi_abs_complex(z);
+ double half_theta = psi_phase_complex(z) / 2.0;
+ double sqrt_r = sqrt(r);
+
+ return (struct PsiComplex)
+ {
+ sqrt_r * cos(half_theta),
+ sqrt_r * sin(half_theta),
+ };
+}
+
+struct PsiComplex psi_add_complex(struct PsiComplex a, struct PsiComplex b)
+{
+ return (struct PsiComplex)
+ {
+ a.real + b.real,
+ a.imaginary + b.imaginary,
+ };
+}
+
+struct PsiComplex psi_sub_complex(struct PsiComplex a, struct PsiComplex b)
+{
+ return (struct PsiComplex)
+ {
+ a.real - b.real,
+ a.imaginary - b.imaginary,
+ };
+}
+
+struct PsiComplex psi_mul_complex(struct PsiComplex a, struct PsiComplex b)
+{
+ // (a + bi)(c + di) = (ac - bd) + (ad + bc)i
+ return (struct PsiComplex)
+ {
+ a.real * b.real - a.imaginary * b.imaginary,
+ a.real * b.imaginary + a.imaginary * b.real,
+ };
+}
+
+struct PsiComplex psi_div_complex(struct PsiComplex a, struct PsiComplex b)
+{
+ // (a + bi) / (c + di) = ((ac + bd) + (bc - ad)i) / (c² + d²)
+ double denom = b.real * b.real + b.imaginary * b.imaginary;
+ return (struct PsiComplex)
+ {
+ (a.real * b.real + a.imaginary * b.imaginary) / denom,
+ (a.imaginary * b.real - a.real * b.imaginary) / denom,
+ };
+}
+
+struct PsiComplex psi_add_complex_real(struct PsiComplex a, double b)
+{
+ return (struct PsiComplex)
+ {
+ a.real + b,
+ a.imaginary,
+ };
+}
+
+struct PsiComplex psi_sub_complex_real(struct PsiComplex a, double b)
+{
+ return (struct PsiComplex)
+ {
+ a.real - b,
+ a.imaginary,
+ };
+}
+
+struct PsiComplex psi_mul_complex_real(struct PsiComplex a, double b)
+{
+ return (struct PsiComplex)
+ {
+ a.real * b,
+ a.imaginary * b,
+ };
+}
+
+struct PsiComplex psi_div_complex_real(struct PsiComplex a, double b)
+{
+ return (struct PsiComplex)
+ {
+ a.real / b,
+ a.imaginary / b,
+ };
+}
diff --git a/src/maths/complex.h b/src/maths/complex.h
new file mode 100644
index 0000000..c94f64a
--- /dev/null
+++ b/src/maths/complex.h
@@ -0,0 +1,28 @@
+#pragma once
+
+struct PsiComplex
+{
+ double real;
+ double imaginary;
+};
+
+struct PsiComplex psi_new_complex(double real, double imaginary);
+struct PsiComplex psi_new_complex_from_real(double real);
+
+struct PsiComplex psi_conjugate_complex(struct PsiComplex z);
+struct PsiComplex psi_neg_complex(struct PsiComplex z);
+
+double psi_phase_complex(struct PsiComplex z);
+double psi_norm2_complex(struct PsiComplex z);
+double psi_abs_complex(struct PsiComplex z);
+struct PsiComplex psi_sqrt_complex(struct PsiComplex z);
+
+struct PsiComplex psi_add_complex(struct PsiComplex a, struct PsiComplex b);
+struct PsiComplex psi_sub_complex(struct PsiComplex a, struct PsiComplex b);
+struct PsiComplex psi_mul_complex(struct PsiComplex a, struct PsiComplex b);
+struct PsiComplex psi_div_complex(struct PsiComplex a, struct PsiComplex b);
+
+struct PsiComplex psi_add_complex_real(struct PsiComplex a, double b);
+struct PsiComplex psi_sub_complex_real(struct PsiComplex a, double b);
+struct PsiComplex psi_mul_complex_real(struct PsiComplex a, double b);
+struct PsiComplex psi_div_complex_real(struct PsiComplex a, double b);
diff --git a/examples/tester/benchmarks.rs b/tester/benchmarks.rs
index 7aea535..7aea535 100644
--- a/examples/tester/benchmarks.rs
+++ b/tester/benchmarks.rs
diff --git a/examples/tester/clifford.rs b/tester/clifford.rs
index 932a918..932a918 100644
--- a/examples/tester/clifford.rs
+++ b/tester/clifford.rs
diff --git a/examples/tester/common.rs b/tester/common.rs
index 9e576da..9e576da 100644
--- a/examples/tester/common.rs
+++ b/tester/common.rs
diff --git a/examples/tester/custom_gates.rs b/tester/custom_gates.rs
index ffe5c01..ffe5c01 100644
--- a/examples/tester/custom_gates.rs
+++ b/tester/custom_gates.rs
diff --git a/examples/tester/kernels.rs b/tester/kernels.rs
index b42b181..b42b181 100644
--- a/examples/tester/kernels.rs
+++ b/tester/kernels.rs
diff --git a/tester/main.c b/tester/main.c
new file mode 100644
index 0000000..eb629fc
--- /dev/null
+++ b/tester/main.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+
+#include "maths/complex.h"
+#include "psi.h"
+
+int main(void)
+{
+ printf("psi %s\n", psi_version());
+
+ struct PsiComplex a = psi_new_complex(1.0, 2.0);
+ struct PsiComplex b = psi_new_complex(3.0, -1.0);
+
+ struct PsiComplex sum = psi_add_complex(a, b);
+ struct PsiComplex product = psi_mul_complex(a, b);
+
+ printf("a = %g + %gi\n", a.real, a.imaginary);
+ printf("b = %g + %gi\n", b.real, b.imaginary);
+ printf("a + b = %g + %gi\n", sum.real, sum.imaginary);
+ printf("a * b = %g + %gi\n", product.real, product.imaginary);
+ printf("|a| = %g\n", psi_abs_complex(a));
+
+ return 0;
+}
diff --git a/examples/tester/main.rs b/tester/main.rs
index ecebd34..ecebd34 100644
--- a/examples/tester/main.rs
+++ b/tester/main.rs
diff --git a/examples/tester/noise.rs b/tester/noise.rs
index 02a4ba3..02a4ba3 100644
--- a/examples/tester/noise.rs
+++ b/tester/noise.rs
diff --git a/examples/tester/non_clifford.rs b/tester/non_clifford.rs
index 2146fb2..2146fb2 100644
--- a/examples/tester/non_clifford.rs
+++ b/tester/non_clifford.rs
diff --git a/examples/tester/simd.rs b/tester/simd.rs
index e91dc6e..e91dc6e 100644
--- a/examples/tester/simd.rs
+++ b/tester/simd.rs