From ab08fa6065b5b08627b51203eb92ef65ed7b5ea2 Mon Sep 17 00:00:00 2001 From: hachem Date: Sun, 6 Sep 2026 18:53:29 +0200 Subject: c: quantum gate and state foundations --- src/core/quantum_components.c | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/core/quantum_components.c (limited to 'src/core') 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 + +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); +} -- cgit v1.3