diff options
| author | hachem <im@hachem.wtf> | 2026-09-06 18:53:29 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-09-06 18:53:29 +0200 |
| commit | ab08fa6065b5b08627b51203eb92ef65ed7b5ea2 (patch) | |
| tree | 09a22fbfcf95a7fd74edeb0dd3af9a6320f66b3a | |
| parent | 565af48c1eff190ce836293a0eda75c0dc695c82 (diff) | |
c: quantum gate and state foundations
| -rw-r--r-- | include/core/quantum_components.h | 20 | ||||
| -rw-r--r-- | src/core/quantum_components.c | 51 | ||||
| -rw-r--r-- | src/maths/matrix.c | 9 | ||||
| -rw-r--r-- | tester/main.c | 17 |
4 files changed, 97 insertions, 0 deletions
diff --git a/include/core/quantum_components.h b/include/core/quantum_components.h new file mode 100644 index 0000000..b0acabc --- /dev/null +++ b/include/core/quantum_components.h @@ -0,0 +1,20 @@ +#pragma once + +#include <stddef.h> + +#include "maths/matrix.h" +#include "maths/vector.h" + +struct PsiVector psi_new_state_0(void); +struct PsiVector psi_new_state_1(void); + +struct PsiQuantumGate +{ + const char *name; + struct PsiMatrix matrix; + size_t num_qubits; +}; + +struct PsiQuantumGate psi_new_quantum_gate(const char *name, struct PsiMatrix matrix, size_t num_qubits); +struct PsiQuantumGate psi_new_quantum_gate_from_matrix(const char *name, struct PsiMatrix matrix); +void psi_free_quantum_gate(struct PsiQuantumGate *gate); diff --git a/src/core/quantum_components.c b/src/core/quantum_components.c new file mode 100644 index 0000000..3b9eef5 --- /dev/null +++ b/src/core/quantum_components.c @@ -0,0 +1,51 @@ +#include "core/quantum_components.h" + +#include <assert.h> + +struct PsiVector psi_new_state_0(void) +{ + return psi_column_vector(psi_new_complex(1.0, 0.0), psi_new_complex(0.0, 0.0)); +} + +struct PsiVector psi_new_state_1(void) +{ + return psi_column_vector(psi_new_complex(0.0, 0.0), psi_new_complex(1.0, 0.0)); +} + +struct PsiQuantumGate psi_new_quantum_gate(const char *name, struct PsiMatrix matrix, size_t num_qubits) +{ + size_t expected_dim = (size_t)1 << num_qubits; + assert(matrix.rows == expected_dim); + assert(matrix.cols == expected_dim); + + return (struct PsiQuantumGate) + { + name, + matrix, + num_qubits, + }; +} + +struct PsiQuantumGate psi_new_quantum_gate_from_matrix(const char *name, struct PsiMatrix matrix) +{ + assert(matrix.rows == matrix.cols); + + size_t dim = matrix.rows; + assert(dim > 0 && (dim & (dim - 1)) == 0); + + size_t num_qubits = 0; + while (((size_t)1 << num_qubits) < dim) + num_qubits++; + + return (struct PsiQuantumGate) + { + name, + matrix, + num_qubits, + }; +} + +void psi_free_quantum_gate(struct PsiQuantumGate *gate) +{ + psi_free_matrix(&gate->matrix); +} diff --git a/src/maths/matrix.c b/src/maths/matrix.c index 400c2a7..44b953c 100644 --- a/src/maths/matrix.c +++ b/src/maths/matrix.c @@ -30,6 +30,15 @@ struct PsiMatrix psi_clone_matrix(struct PsiMatrix m) return psi_new_matrix_from(m.data, m.rows, m.cols); } +struct PsiMatrix psi_identity_matrix(size_t size) +{ + struct PsiMatrix m = psi_new_matrix(size, size); + for (size_t i = 0; i < size; i++) + m.data[i * size + i] = psi_new_complex(1.0, 0.0); + + return m; +} + void psi_free_matrix(struct PsiMatrix *m) { free(m->data); diff --git a/tester/main.c b/tester/main.c index 334291f..cba5d28 100644 --- a/tester/main.c +++ b/tester/main.c @@ -1,5 +1,6 @@ #include <stdio.h> +#include "core/quantum_components.h" #include "maths/complex.h" #include "maths/matrix.h" #include "maths/vector.h" @@ -60,5 +61,21 @@ int main(void) psi_free_matrix(&m); psi_free_matrix(&mm); + double inv_sqrt2 = 0.7071067811865475; + struct PsiMatrix h = psi_matrix(2, 2, + psi_new_complex(inv_sqrt2, 0.0), psi_new_complex(inv_sqrt2, 0.0), + psi_new_complex(inv_sqrt2, 0.0), psi_new_complex(-inv_sqrt2, 0.0)); + struct PsiQuantumGate gate = psi_new_quantum_gate_from_matrix("H", h); + + struct PsiVector s0 = psi_new_state_0(); + struct PsiVector hs0 = psi_mul_vector_matrix(s0, gate.matrix); + + printf("gate = %s (%zu qubit)\n", gate.name, gate.num_qubits); + printf("H|0> = [%g, %g]\n", hs0.data[0].real, hs0.data[1].real); + + psi_free_vector(&s0); + psi_free_vector(&hs0); + psi_free_quantum_gate(&gate); + return 0; } |
