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 /src/core | |
| parent | 565af48c1eff190ce836293a0eda75c0dc695c82 (diff) | |
c: quantum gate and state foundations
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/quantum_components.c | 51 |
1 files changed, 51 insertions, 0 deletions
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); +} |
