aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-09-18 12:25:32 +0200
committerhachem <im@hachem.wtf>2026-09-18 12:25:32 +0200
commit17598056a69a14e0390a07251d383f413ded9eea (patch)
tree79cf23ea99072f065407da3e0b92ee03f8972695 /include
parentee14ad272e68d9363202d7f668e0b20302827209 (diff)
feat: add matrix, vector, and circuit display + fmt
Diffstat (limited to 'include')
-rw-r--r--include/core/circuit.h91
-rw-r--r--include/core/classical_components.h10
-rw-r--r--include/core/custom_gate.h56
-rw-r--r--include/core/kernel.h58
-rw-r--r--include/core/noise.h18
-rw-r--r--include/core/quantum_components.h18
-rw-r--r--include/core/runtime.h26
-rw-r--r--include/maths/complex.h4
-rw-r--r--include/maths/format.h6
-rw-r--r--include/maths/matrix.h8
-rw-r--r--include/maths/simd.h8
-rw-r--r--include/maths/vector.h22
12 files changed, 167 insertions, 158 deletions
diff --git a/include/core/circuit.h b/include/core/circuit.h
index 7c0d371..ce36b1c 100644
--- a/include/core/circuit.h
+++ b/include/core/circuit.h
@@ -2,53 +2,54 @@
#include <stdbool.h>
#include <stddef.h>
+#include <stdio.h>
#include "core/custom_gate.h"
#include "maths/vector.h"
enum PsiGateKind
{
- PSI_GATE_H,
- PSI_GATE_X,
- PSI_GATE_Y,
- PSI_GATE_Z,
- PSI_GATE_S,
- PSI_GATE_T,
- PSI_GATE_SDG,
- PSI_GATE_TDG,
- PSI_GATE_SX,
- PSI_GATE_SXDG,
- PSI_GATE_RX,
- PSI_GATE_RY,
- PSI_GATE_RZ,
- PSI_GATE_P,
- PSI_GATE_U1,
- PSI_GATE_U2,
- PSI_GATE_U3,
- PSI_GATE_CNOT,
- PSI_GATE_CZ,
- PSI_GATE_SWAP,
- PSI_GATE_CRX,
- PSI_GATE_CRY,
- PSI_GATE_CRZ,
- PSI_GATE_CP,
- PSI_GATE_CCNOT,
- PSI_GATE_CSWAP,
- PSI_GATE_MEASURE,
- PSI_GATE_CUSTOM,
+ PSI_GATE_H,
+ PSI_GATE_X,
+ PSI_GATE_Y,
+ PSI_GATE_Z,
+ PSI_GATE_S,
+ PSI_GATE_T,
+ PSI_GATE_SDG,
+ PSI_GATE_TDG,
+ PSI_GATE_SX,
+ PSI_GATE_SXDG,
+ PSI_GATE_RX,
+ PSI_GATE_RY,
+ PSI_GATE_RZ,
+ PSI_GATE_P,
+ PSI_GATE_U1,
+ PSI_GATE_U2,
+ PSI_GATE_U3,
+ PSI_GATE_CNOT,
+ PSI_GATE_CZ,
+ PSI_GATE_SWAP,
+ PSI_GATE_CRX,
+ PSI_GATE_CRY,
+ PSI_GATE_CRZ,
+ PSI_GATE_CP,
+ PSI_GATE_CCNOT,
+ PSI_GATE_CSWAP,
+ PSI_GATE_MEASURE,
+ PSI_GATE_CUSTOM,
};
struct PsiGateOp
{
- enum PsiGateKind kind;
- size_t qubits[3];
- size_t qubit_count;
- double params[3];
- size_t param_count;
- size_t classical;
- struct PsiCustomGate* custom;
- size_t* custom_targets;
- size_t custom_target_count;
+ enum PsiGateKind kind;
+ size_t qubits[3];
+ size_t qubit_count;
+ double params[3];
+ size_t param_count;
+ size_t classical;
+ struct PsiCustomGate* custom;
+ size_t* custom_targets;
+ size_t custom_target_count;
};
const char* psi_gate_op_name(struct PsiGateOp op);
@@ -60,13 +61,13 @@ bool psi_gate_op_is_non_clifford(struct PsiGateOp op);
struct PsiQuantumCircuit
{
- size_t num_qubits;
- size_t num_classical;
- struct PsiGateOp* operations;
- size_t operation_count;
- size_t operation_capacity;
- struct PsiVector computed_state;
- bool is_computed;
+ size_t num_qubits;
+ size_t num_classical;
+ struct PsiGateOp* operations;
+ size_t operation_count;
+ size_t operation_capacity;
+ struct PsiVector computed_state;
+ bool is_computed;
};
struct PsiQuantumCircuit psi_new_quantum_circuit(size_t num_qubits);
@@ -111,3 +112,5 @@ void psi_measure_all(struct PsiQuantumCircuit* c);
void psi_apply_custom(struct PsiQuantumCircuit* c, struct PsiCustomGate gate, const size_t* targets,
size_t count);
+
+void psi_print_circuit(const struct PsiQuantumCircuit* circuit, FILE* out);
diff --git a/include/core/classical_components.h b/include/core/classical_components.h
index 53c00ef..7c58ae3 100644
--- a/include/core/classical_components.h
+++ b/include/core/classical_components.h
@@ -5,15 +5,15 @@
struct PsiClassicalBit
{
- const char* name;
- bool state;
+ const char* name;
+ bool state;
};
struct PsiClassicalRegister
{
- const char* name;
- struct PsiClassicalBit* bits;
- size_t num_bits;
+ const char* name;
+ struct PsiClassicalBit* bits;
+ size_t num_bits;
};
struct PsiClassicalBit psi_new_classical_bit(const char* name, bool state);
diff --git a/include/core/custom_gate.h b/include/core/custom_gate.h
index efda36f..7003699 100644
--- a/include/core/custom_gate.h
+++ b/include/core/custom_gate.h
@@ -7,46 +7,46 @@
enum PsiCompositeOp
{
- PSI_OP_H,
- PSI_OP_X,
- PSI_OP_Y,
- PSI_OP_Z,
- PSI_OP_S,
- PSI_OP_T,
- PSI_OP_CNOT,
- PSI_OP_CZ,
- PSI_OP_SWAP,
- PSI_OP_CCNOT,
- PSI_OP_CSWAP,
+ PSI_OP_H,
+ PSI_OP_X,
+ PSI_OP_Y,
+ PSI_OP_Z,
+ PSI_OP_S,
+ PSI_OP_T,
+ PSI_OP_CNOT,
+ PSI_OP_CZ,
+ PSI_OP_SWAP,
+ PSI_OP_CCNOT,
+ PSI_OP_CSWAP,
};
struct PsiCompositeGateOp
{
- enum PsiCompositeOp op;
- size_t targets[3];
- size_t target_count;
+ enum PsiCompositeOp op;
+ size_t targets[3];
+ size_t target_count;
};
enum PsiCustomGateKind
{
- PSI_CUSTOM_GATE_MATRIX,
- PSI_CUSTOM_GATE_COMPOSITE,
+ PSI_CUSTOM_GATE_MATRIX,
+ PSI_CUSTOM_GATE_COMPOSITE,
};
struct PsiCustomGate
{
- const char* name;
- size_t num_qubits;
- enum PsiCustomGateKind kind;
- union
- {
- struct PsiMatrix matrix;
- struct
- {
- struct PsiCompositeGateOp* ops;
- size_t count;
- } composite;
- } definition;
+ const char* name;
+ size_t num_qubits;
+ enum PsiCustomGateKind kind;
+ union
+ {
+ struct PsiMatrix matrix;
+ struct
+ {
+ struct PsiCompositeGateOp* ops;
+ size_t count;
+ } composite;
+ } definition;
};
struct PsiCustomGate psi_new_custom_gate_from_matrix(const char* name, struct PsiMatrix matrix);
diff --git a/include/core/kernel.h b/include/core/kernel.h
index b8b19ea..f3a2b77 100644
--- a/include/core/kernel.h
+++ b/include/core/kernel.h
@@ -8,18 +8,18 @@
enum PsiGateType
{
- PSI_GATE_TYPE_DIAGONAL,
- PSI_GATE_TYPE_NON_DIAGONAL,
- PSI_GATE_TYPE_CONTROLLED,
+ PSI_GATE_TYPE_DIAGONAL,
+ PSI_GATE_TYPE_NON_DIAGONAL,
+ PSI_GATE_TYPE_CONTROLLED,
};
struct PsiKernel
{
- struct PsiMatrix matrix;
- size_t* targets;
- size_t target_count;
- char* name;
- enum PsiGateType gate_type;
+ struct PsiMatrix matrix;
+ size_t* targets;
+ size_t target_count;
+ char* name;
+ enum PsiGateType gate_type;
};
struct PsiKernel psi_new_kernel(const char* name, struct PsiMatrix matrix, const size_t* targets,
@@ -34,10 +34,10 @@ bool psi_fuse_kernels(struct PsiKernel a, struct PsiKernel b, struct PsiKernel*
struct PsiKernelBatch
{
- struct PsiKernel* kernels;
- size_t count;
- size_t capacity;
- size_t num_qubits;
+ struct PsiKernel* kernels;
+ size_t count;
+ size_t capacity;
+ size_t num_qubits;
};
struct PsiKernelBatch psi_new_kernel_batch(size_t num_qubits);
@@ -49,31 +49,31 @@ void psi_apply_kernel(struct PsiVector* state, struct PsiKernel kernel, size_t n
struct PsiExecutionLayer
{
- struct PsiKernel* kernels;
- size_t count;
- size_t capacity;
+ struct PsiKernel* kernels;
+ size_t count;
+ size_t capacity;
};
struct PsiKernelStats
{
- size_t total_kernels;
- size_t single_qubit;
- size_t two_qubit;
- size_t multi_qubit;
- size_t diagonal;
- size_t execution_layers;
+ size_t total_kernels;
+ size_t single_qubit;
+ size_t two_qubit;
+ size_t multi_qubit;
+ size_t diagonal;
+ size_t execution_layers;
};
struct PsiStructureAwareBatch
{
- struct PsiKernel* kernels;
- size_t count;
- size_t capacity;
- struct PsiExecutionLayer* layers;
- size_t layer_count;
- size_t layer_capacity;
- size_t num_qubits;
- bool optimised;
+ struct PsiKernel* kernels;
+ size_t count;
+ size_t capacity;
+ struct PsiExecutionLayer* layers;
+ size_t layer_count;
+ size_t layer_capacity;
+ size_t num_qubits;
+ bool optimised;
};
struct PsiStructureAwareBatch psi_new_structure_aware_batch(size_t num_qubits);
diff --git a/include/core/noise.h b/include/core/noise.h
index 320a4ab..2bd65e1 100644
--- a/include/core/noise.h
+++ b/include/core/noise.h
@@ -8,8 +8,8 @@
struct PsiKrausOperator
{
- struct PsiMatrix matrix;
- const char* name;
+ struct PsiMatrix matrix;
+ const char* name;
};
struct PsiKrausOperator psi_new_kraus_operator(const char* name, struct PsiMatrix matrix);
@@ -17,10 +17,10 @@ void psi_free_kraus_operator(struct PsiKrausOperator* op);
struct PsiNoiseChannel
{
- const char* name;
- struct PsiKrausOperator* operators;
- size_t operator_count;
- size_t num_qubits;
+ const char* name;
+ struct PsiKrausOperator* operators;
+ size_t operator_count;
+ size_t num_qubits;
};
struct PsiNoiseChannel psi_new_noise_channel(const char* name,
@@ -38,9 +38,9 @@ struct PsiNoiseChannel psi_generalised_amplitude_damping_channel(double p, doubl
struct PsiDensityMatrix
{
- struct PsiComplex* data;
- size_t dim;
- size_t num_qubits;
+ struct PsiComplex* data;
+ size_t dim;
+ size_t num_qubits;
};
struct PsiDensityMatrix psi_new_density_matrix(size_t num_qubits);
diff --git a/include/core/quantum_components.h b/include/core/quantum_components.h
index 63797cf..815e2d9 100644
--- a/include/core/quantum_components.h
+++ b/include/core/quantum_components.h
@@ -10,9 +10,9 @@ struct PsiVector psi_new_state_1(void);
struct PsiQuantumGate
{
- const char* name;
- struct PsiMatrix matrix;
- size_t num_qubits;
+ const char* name;
+ struct PsiMatrix matrix;
+ size_t num_qubits;
};
struct PsiQuantumGate psi_new_quantum_gate(const char* name, struct PsiMatrix matrix,
@@ -22,8 +22,8 @@ void psi_free_quantum_gate(struct PsiQuantumGate* gate);
struct PsiQuantumBit
{
- const char* name;
- struct PsiVector state;
+ const char* name;
+ struct PsiVector state;
};
struct PsiQuantumBit psi_new_quantum_bit(const char* name, struct PsiVector state);
@@ -31,10 +31,10 @@ void psi_free_quantum_bit(struct PsiQuantumBit* bit);
struct PsiQuantumRegister
{
- const char* name;
- struct PsiVector state_vector;
- struct PsiQuantumBit* qubits;
- size_t num_qubits;
+ const char* name;
+ struct PsiVector state_vector;
+ struct PsiQuantumBit* qubits;
+ size_t num_qubits;
};
struct PsiQuantumRegister psi_new_quantum_register(const char* name, const char** names,
diff --git a/include/core/runtime.h b/include/core/runtime.h
index 2a6f739..cf03371 100644
--- a/include/core/runtime.h
+++ b/include/core/runtime.h
@@ -10,23 +10,23 @@
struct PsiRuntimeConfig
{
- bool parallel;
- bool simd;
- bool batched;
- bool structure_aware;
- size_t parallel_threshold;
+ bool parallel;
+ bool simd;
+ bool batched;
+ bool structure_aware;
+ size_t parallel_threshold;
};
enum PsiRuntime
{
- PSI_RUNTIME_BASIC,
- PSI_RUNTIME_BASIC_MT,
- PSI_RUNTIME_BATCHED,
- PSI_RUNTIME_BATCHED_MT,
- PSI_RUNTIME_SIMD,
- PSI_RUNTIME_SIMD_MT,
- PSI_RUNTIME_STRUCTURE_AWARE,
- PSI_RUNTIME_STRUCTURE_AWARE_MT,
+ PSI_RUNTIME_BASIC,
+ PSI_RUNTIME_BASIC_MT,
+ PSI_RUNTIME_BATCHED,
+ PSI_RUNTIME_BATCHED_MT,
+ PSI_RUNTIME_SIMD,
+ PSI_RUNTIME_SIMD_MT,
+ PSI_RUNTIME_STRUCTURE_AWARE,
+ PSI_RUNTIME_STRUCTURE_AWARE_MT,
};
struct PsiRuntimeConfig psi_new_runtime_config(void);
diff --git a/include/maths/complex.h b/include/maths/complex.h
index c94f64a..d2782eb 100644
--- a/include/maths/complex.h
+++ b/include/maths/complex.h
@@ -2,8 +2,8 @@
struct PsiComplex
{
- double real;
- double imaginary;
+ double real;
+ double imaginary;
};
struct PsiComplex psi_new_complex(double real, double imaginary);
diff --git a/include/maths/format.h b/include/maths/format.h
index a11e3be..3b746a5 100644
--- a/include/maths/format.h
+++ b/include/maths/format.h
@@ -1,8 +1,14 @@
#pragma once
#include <stddef.h>
+#include <stdio.h>
#include "maths/complex.h"
+#include "maths/matrix.h"
+#include "maths/vector.h"
char* psi_format_amplitude(struct PsiComplex c, char* out, size_t cap);
char* psi_format_probability(double p, char* out, size_t cap);
+
+void psi_print_matrix(struct PsiMatrix m, FILE* out);
+void psi_print_vector(struct PsiVector v, FILE* out);
diff --git a/include/maths/matrix.h b/include/maths/matrix.h
index ff996d6..87bdcee 100644
--- a/include/maths/matrix.h
+++ b/include/maths/matrix.h
@@ -7,9 +7,9 @@
struct PsiMatrix
{
- struct PsiComplex* data;
- size_t rows;
- size_t cols;
+ struct PsiComplex* data;
+ size_t rows;
+ size_t cols;
};
struct PsiMatrix psi_new_matrix(size_t rows, size_t cols);
@@ -19,7 +19,7 @@ struct PsiMatrix psi_clone_matrix(struct PsiMatrix m);
void psi_free_matrix(struct PsiMatrix* m);
#define psi_matrix(rows, cols, ...) \
- psi_new_matrix_from((struct PsiComplex[]){ __VA_ARGS__ }, rows, cols)
+ psi_new_matrix_from((struct PsiComplex[]){ __VA_ARGS__ }, rows, cols)
struct PsiComplex psi_get_matrix(struct PsiMatrix m, size_t row, size_t col);
void psi_set_matrix(struct PsiMatrix* m, size_t row, size_t col, struct PsiComplex value);
diff --git a/include/maths/simd.h b/include/maths/simd.h
index 38d0171..5533913 100644
--- a/include/maths/simd.h
+++ b/include/maths/simd.h
@@ -6,10 +6,10 @@
enum PsiSimdCapability
{
- PSI_SIMD_NONE,
- PSI_SIMD_AVX2,
- PSI_SIMD_AVX512,
- PSI_SIMD_NEON,
+ PSI_SIMD_NONE,
+ PSI_SIMD_AVX2,
+ PSI_SIMD_AVX512,
+ PSI_SIMD_NEON,
};
enum PsiSimdCapability psi_detect_simd(void);
diff --git a/include/maths/vector.h b/include/maths/vector.h
index 0136715..a9ddf1f 100644
--- a/include/maths/vector.h
+++ b/include/maths/vector.h
@@ -6,15 +6,15 @@
enum PsiVectorKind
{
- PSI_ROW_VECTOR,
- PSI_COLUMN_VECTOR,
+ PSI_ROW_VECTOR,
+ PSI_COLUMN_VECTOR,
};
struct PsiVector
{
- struct PsiComplex* data;
- size_t size;
- enum PsiVectorKind kind;
+ struct PsiComplex* data;
+ size_t size;
+ enum PsiVectorKind kind;
};
struct PsiVector psi_new_vector(size_t size, enum PsiVectorKind kind);
@@ -24,13 +24,13 @@ struct PsiVector psi_clone_vector(struct PsiVector v);
void psi_free_vector(struct PsiVector* v);
#define psi_row_vector(...) \
- psi_new_vector_from((struct PsiComplex[]){ __VA_ARGS__ }, \
- sizeof((struct PsiComplex[]){ __VA_ARGS__ }) / sizeof(struct PsiComplex), \
- PSI_ROW_VECTOR)
+ psi_new_vector_from((struct PsiComplex[]){ __VA_ARGS__ }, \
+ sizeof((struct PsiComplex[]){ __VA_ARGS__ }) / sizeof(struct PsiComplex), \
+ PSI_ROW_VECTOR)
#define psi_column_vector(...) \
- psi_new_vector_from((struct PsiComplex[]){ __VA_ARGS__ }, \
- sizeof((struct PsiComplex[]){ __VA_ARGS__ }) / sizeof(struct PsiComplex), \
- PSI_COLUMN_VECTOR)
+ psi_new_vector_from((struct PsiComplex[]){ __VA_ARGS__ }, \
+ sizeof((struct PsiComplex[]){ __VA_ARGS__ }) / sizeof(struct PsiComplex), \
+ PSI_COLUMN_VECTOR)
struct PsiComplex psi_get_vector(struct PsiVector v, size_t index);
void psi_set_vector(struct PsiVector* v, size_t index, struct PsiComplex value);