diff options
Diffstat (limited to 'src/core/circuit.c')
| -rw-r--r-- | src/core/circuit.c | 194 |
1 files changed, 135 insertions, 59 deletions
diff --git a/src/core/circuit.c b/src/core/circuit.c index 1869ee6..8009db5 100644 --- a/src/core/circuit.c +++ b/src/core/circuit.c @@ -4,7 +4,7 @@ #include <stdlib.h> #include <string.h> -const char *psi_gate_op_name(struct PsiGateOp op) +const char* psi_gate_op_name(struct PsiGateOp op) { switch (op.kind) { @@ -41,7 +41,7 @@ const char *psi_gate_op_name(struct PsiGateOp op) return "?"; } -const size_t *psi_gate_op_quantum_targets(const struct PsiGateOp *op, size_t *out_count) +const size_t* psi_gate_op_quantum_targets(const struct PsiGateOp* op, size_t* out_count) { if (op->kind == PSI_GATE_CUSTOM) { @@ -53,7 +53,7 @@ const size_t *psi_gate_op_quantum_targets(const struct PsiGateOp *op, size_t *ou return op->qubits; } -const size_t *psi_gate_op_classical_targets(const struct PsiGateOp *op, size_t *out_count) +const size_t* psi_gate_op_classical_targets(const struct PsiGateOp* op, size_t* out_count) { if (op->kind == PSI_GATE_MEASURE) { @@ -93,10 +93,8 @@ bool psi_gate_op_is_non_clifford(struct PsiGateOp op) case PSI_GATE_CRX: case PSI_GATE_CRY: case PSI_GATE_CRZ: - case PSI_GATE_CP: - return true; - default: - return false; + case PSI_GATE_CP: return true; + default: return false; } } @@ -105,7 +103,8 @@ struct PsiQuantumCircuit psi_new_quantum_circuit(size_t num_qubits) return psi_new_quantum_circuit_with_classical(num_qubits, 0); } -struct PsiQuantumCircuit psi_new_quantum_circuit_with_classical(size_t num_qubits, size_t num_classical) +struct PsiQuantumCircuit psi_new_quantum_circuit_with_classical(size_t num_qubits, + size_t num_classical) { struct PsiQuantumCircuit c; c.num_qubits = num_qubits; @@ -119,11 +118,11 @@ struct PsiQuantumCircuit psi_new_quantum_circuit_with_classical(size_t num_qubit return c; } -static void free_operations(struct PsiQuantumCircuit *c) +static void free_operations(struct PsiQuantumCircuit* c) { for (size_t i = 0; i < c->operation_count; i++) { - struct PsiGateOp *op = &c->operations[i]; + struct PsiGateOp* op = &c->operations[i]; if (op->kind != PSI_GATE_CUSTOM) continue; @@ -133,7 +132,7 @@ static void free_operations(struct PsiQuantumCircuit *c) } } -void psi_free_quantum_circuit(struct PsiQuantumCircuit *c) +void psi_free_quantum_circuit(struct PsiQuantumCircuit* c) { free_operations(c); free(c->operations); @@ -144,7 +143,7 @@ void psi_free_quantum_circuit(struct PsiQuantumCircuit *c) c->is_computed = false; } -void psi_reset_circuit(struct PsiQuantumCircuit *c) +void psi_reset_circuit(struct PsiQuantumCircuit* c) { free_operations(c); c->operation_count = 0; @@ -153,7 +152,7 @@ void psi_reset_circuit(struct PsiQuantumCircuit *c) c->is_computed = false; } -static struct PsiGateOp *append_op(struct PsiQuantumCircuit *c) +static struct PsiGateOp* append_op(struct PsiQuantumCircuit* c) { if (c->operation_count == c->operation_capacity) { @@ -163,24 +162,25 @@ static struct PsiGateOp *append_op(struct PsiQuantumCircuit *c) c->operation_capacity = new_capacity; } - struct PsiGateOp *op = &c->operations[c->operation_count++]; + struct PsiGateOp* op = &c->operations[c->operation_count++]; memset(op, 0, sizeof(*op)); c->is_computed = false; return op; } -static void push_1q(struct PsiQuantumCircuit *c, enum PsiGateKind kind, size_t target) +static void push_1q(struct PsiQuantumCircuit* c, enum PsiGateKind kind, size_t target) { - struct PsiGateOp *op = append_op(c); + struct PsiGateOp* op = append_op(c); op->kind = kind; op->qubits[0] = target; op->qubit_count = 1; } -static void push_1q_1p(struct PsiQuantumCircuit *c, enum PsiGateKind kind, size_t target, double theta) +static void push_1q_1p(struct PsiQuantumCircuit* c, enum PsiGateKind kind, size_t target, + double theta) { - struct PsiGateOp *op = append_op(c); + struct PsiGateOp* op = append_op(c); op->kind = kind; op->qubits[0] = target; op->qubit_count = 1; @@ -188,18 +188,19 @@ static void push_1q_1p(struct PsiQuantumCircuit *c, enum PsiGateKind kind, size_ op->param_count = 1; } -static void push_2q(struct PsiQuantumCircuit *c, enum PsiGateKind kind, size_t a, size_t b) +static void push_2q(struct PsiQuantumCircuit* c, enum PsiGateKind kind, size_t a, size_t b) { - struct PsiGateOp *op = append_op(c); + struct PsiGateOp* op = append_op(c); op->kind = kind; op->qubits[0] = a; op->qubits[1] = b; op->qubit_count = 2; } -static void push_2q_1p(struct PsiQuantumCircuit *c, enum PsiGateKind kind, size_t control, size_t target, double theta) +static void push_2q_1p(struct PsiQuantumCircuit* c, enum PsiGateKind kind, size_t control, + size_t target, double theta) { - struct PsiGateOp *op = append_op(c); + struct PsiGateOp* op = append_op(c); op->kind = kind; op->qubits[0] = control; op->qubits[1] = target; @@ -208,9 +209,10 @@ static void push_2q_1p(struct PsiQuantumCircuit *c, enum PsiGateKind kind, size_ op->param_count = 1; } -static void push_3q(struct PsiQuantumCircuit *c, enum PsiGateKind kind, size_t a, size_t b, size_t d) +static void push_3q(struct PsiQuantumCircuit* c, enum PsiGateKind kind, size_t a, size_t b, + size_t d) { - struct PsiGateOp *op = append_op(c); + struct PsiGateOp* op = append_op(c); op->kind = kind; op->qubits[0] = a; op->qubits[1] = b; @@ -218,26 +220,71 @@ static void push_3q(struct PsiQuantumCircuit *c, enum PsiGateKind kind, size_t a op->qubit_count = 3; } -void psi_apply_h(struct PsiQuantumCircuit *c, size_t target) { push_1q(c, PSI_GATE_H, target); } -void psi_apply_x(struct PsiQuantumCircuit *c, size_t target) { push_1q(c, PSI_GATE_X, target); } -void psi_apply_y(struct PsiQuantumCircuit *c, size_t target) { push_1q(c, PSI_GATE_Y, target); } -void psi_apply_z(struct PsiQuantumCircuit *c, size_t target) { push_1q(c, PSI_GATE_Z, target); } -void psi_apply_s(struct PsiQuantumCircuit *c, size_t target) { push_1q(c, PSI_GATE_S, target); } -void psi_apply_t(struct PsiQuantumCircuit *c, size_t target) { push_1q(c, PSI_GATE_T, target); } -void psi_apply_sdg(struct PsiQuantumCircuit *c, size_t target) { push_1q(c, PSI_GATE_SDG, target); } -void psi_apply_tdg(struct PsiQuantumCircuit *c, size_t target) { push_1q(c, PSI_GATE_TDG, target); } -void psi_apply_sx(struct PsiQuantumCircuit *c, size_t target) { push_1q(c, PSI_GATE_SX, target); } -void psi_apply_sxdg(struct PsiQuantumCircuit *c, size_t target) { push_1q(c, PSI_GATE_SXDG, target); } +void psi_apply_h(struct PsiQuantumCircuit* c, size_t target) +{ + push_1q(c, PSI_GATE_H, target); +} +void psi_apply_x(struct PsiQuantumCircuit* c, size_t target) +{ + push_1q(c, PSI_GATE_X, target); +} +void psi_apply_y(struct PsiQuantumCircuit* c, size_t target) +{ + push_1q(c, PSI_GATE_Y, target); +} +void psi_apply_z(struct PsiQuantumCircuit* c, size_t target) +{ + push_1q(c, PSI_GATE_Z, target); +} +void psi_apply_s(struct PsiQuantumCircuit* c, size_t target) +{ + push_1q(c, PSI_GATE_S, target); +} +void psi_apply_t(struct PsiQuantumCircuit* c, size_t target) +{ + push_1q(c, PSI_GATE_T, target); +} +void psi_apply_sdg(struct PsiQuantumCircuit* c, size_t target) +{ + push_1q(c, PSI_GATE_SDG, target); +} +void psi_apply_tdg(struct PsiQuantumCircuit* c, size_t target) +{ + push_1q(c, PSI_GATE_TDG, target); +} +void psi_apply_sx(struct PsiQuantumCircuit* c, size_t target) +{ + push_1q(c, PSI_GATE_SX, target); +} +void psi_apply_sxdg(struct PsiQuantumCircuit* c, size_t target) +{ + push_1q(c, PSI_GATE_SXDG, target); +} -void psi_apply_rx(struct PsiQuantumCircuit *c, size_t target, double theta) { push_1q_1p(c, PSI_GATE_RX, target, theta); } -void psi_apply_ry(struct PsiQuantumCircuit *c, size_t target, double theta) { push_1q_1p(c, PSI_GATE_RY, target, theta); } -void psi_apply_rz(struct PsiQuantumCircuit *c, size_t target, double theta) { push_1q_1p(c, PSI_GATE_RZ, target, theta); } -void psi_apply_p(struct PsiQuantumCircuit *c, size_t target, double theta) { push_1q_1p(c, PSI_GATE_P, target, theta); } -void psi_apply_u1(struct PsiQuantumCircuit *c, size_t target, double lambda) { push_1q_1p(c, PSI_GATE_U1, target, lambda); } +void psi_apply_rx(struct PsiQuantumCircuit* c, size_t target, double theta) +{ + push_1q_1p(c, PSI_GATE_RX, target, theta); +} +void psi_apply_ry(struct PsiQuantumCircuit* c, size_t target, double theta) +{ + push_1q_1p(c, PSI_GATE_RY, target, theta); +} +void psi_apply_rz(struct PsiQuantumCircuit* c, size_t target, double theta) +{ + push_1q_1p(c, PSI_GATE_RZ, target, theta); +} +void psi_apply_p(struct PsiQuantumCircuit* c, size_t target, double theta) +{ + push_1q_1p(c, PSI_GATE_P, target, theta); +} +void psi_apply_u1(struct PsiQuantumCircuit* c, size_t target, double lambda) +{ + push_1q_1p(c, PSI_GATE_U1, target, lambda); +} -void psi_apply_u2(struct PsiQuantumCircuit *c, size_t target, double phi, double lambda) +void psi_apply_u2(struct PsiQuantumCircuit* c, size_t target, double phi, double lambda) { - struct PsiGateOp *op = append_op(c); + struct PsiGateOp* op = append_op(c); op->kind = PSI_GATE_U2; op->qubits[0] = target; op->qubit_count = 1; @@ -246,9 +293,10 @@ void psi_apply_u2(struct PsiQuantumCircuit *c, size_t target, double phi, double op->param_count = 2; } -void psi_apply_u3(struct PsiQuantumCircuit *c, size_t target, double theta, double phi, double lambda) +void psi_apply_u3(struct PsiQuantumCircuit* c, size_t target, double theta, double phi, + double lambda) { - struct PsiGateOp *op = append_op(c); + struct PsiGateOp* op = append_op(c); op->kind = PSI_GATE_U3; op->qubits[0] = target; op->qubit_count = 1; @@ -258,47 +306,75 @@ void psi_apply_u3(struct PsiQuantumCircuit *c, size_t target, double theta, doub op->param_count = 3; } -void psi_apply_cnot(struct PsiQuantumCircuit *c, size_t control, size_t target) { push_2q(c, PSI_GATE_CNOT, control, target); } -void psi_apply_cz(struct PsiQuantumCircuit *c, size_t control, size_t target) { push_2q(c, PSI_GATE_CZ, control, target); } -void psi_apply_swap(struct PsiQuantumCircuit *c, size_t qubit1, size_t qubit2) { push_2q(c, PSI_GATE_SWAP, qubit1, qubit2); } -void psi_apply_crx(struct PsiQuantumCircuit *c, size_t control, size_t target, double theta) { push_2q_1p(c, PSI_GATE_CRX, control, target, theta); } -void psi_apply_cry(struct PsiQuantumCircuit *c, size_t control, size_t target, double theta) { push_2q_1p(c, PSI_GATE_CRY, control, target, theta); } -void psi_apply_crz(struct PsiQuantumCircuit *c, size_t control, size_t target, double theta) { push_2q_1p(c, PSI_GATE_CRZ, control, target, theta); } -void psi_apply_cp(struct PsiQuantumCircuit *c, size_t control, size_t target, double theta) { push_2q_1p(c, PSI_GATE_CP, control, target, theta); } +void psi_apply_cnot(struct PsiQuantumCircuit* c, size_t control, size_t target) +{ + push_2q(c, PSI_GATE_CNOT, control, target); +} +void psi_apply_cz(struct PsiQuantumCircuit* c, size_t control, size_t target) +{ + push_2q(c, PSI_GATE_CZ, control, target); +} +void psi_apply_swap(struct PsiQuantumCircuit* c, size_t qubit1, size_t qubit2) +{ + push_2q(c, PSI_GATE_SWAP, qubit1, qubit2); +} +void psi_apply_crx(struct PsiQuantumCircuit* c, size_t control, size_t target, double theta) +{ + push_2q_1p(c, PSI_GATE_CRX, control, target, theta); +} +void psi_apply_cry(struct PsiQuantumCircuit* c, size_t control, size_t target, double theta) +{ + push_2q_1p(c, PSI_GATE_CRY, control, target, theta); +} +void psi_apply_crz(struct PsiQuantumCircuit* c, size_t control, size_t target, double theta) +{ + push_2q_1p(c, PSI_GATE_CRZ, control, target, theta); +} +void psi_apply_cp(struct PsiQuantumCircuit* c, size_t control, size_t target, double theta) +{ + push_2q_1p(c, PSI_GATE_CP, control, target, theta); +} -void psi_apply_ccnot(struct PsiQuantumCircuit *c, size_t control1, size_t control2, size_t target) { push_3q(c, PSI_GATE_CCNOT, control1, control2, target); } -void psi_apply_cswap(struct PsiQuantumCircuit *c, size_t control, size_t target1, size_t target2) { push_3q(c, PSI_GATE_CSWAP, control, target1, target2); } +void psi_apply_ccnot(struct PsiQuantumCircuit* c, size_t control1, size_t control2, size_t target) +{ + push_3q(c, PSI_GATE_CCNOT, control1, control2, target); +} +void psi_apply_cswap(struct PsiQuantumCircuit* c, size_t control, size_t target1, size_t target2) +{ + push_3q(c, PSI_GATE_CSWAP, control, target1, target2); +} -void psi_measure(struct PsiQuantumCircuit *c, size_t qubit, size_t classical) +void psi_measure(struct PsiQuantumCircuit* c, size_t qubit, size_t classical) { if (classical >= c->num_classical) c->num_classical = classical + 1; - struct PsiGateOp *op = append_op(c); + struct PsiGateOp* op = append_op(c); op->kind = PSI_GATE_MEASURE; op->qubits[0] = qubit; op->qubit_count = 1; op->classical = classical; } -void psi_measure_all(struct PsiQuantumCircuit *c) +void psi_measure_all(struct PsiQuantumCircuit* c) { for (size_t i = 0; i < c->num_qubits; i++) psi_measure(c, i, i); } -void psi_apply_custom(struct PsiQuantumCircuit *c, struct PsiCustomGate gate, const size_t *targets, size_t count) +void psi_apply_custom(struct PsiQuantumCircuit* c, struct PsiCustomGate gate, const size_t* targets, + size_t count) { - struct PsiCustomGate *owned = malloc(sizeof(struct PsiCustomGate)); + struct PsiCustomGate* owned = malloc(sizeof(struct PsiCustomGate)); assert(owned != NULL); *owned = gate; - size_t *owned_targets = malloc(count * sizeof(size_t)); + size_t* owned_targets = malloc(count * sizeof(size_t)); assert(owned_targets != NULL || count == 0); if (count > 0) memcpy(owned_targets, targets, count * sizeof(size_t)); - struct PsiGateOp *op = append_op(c); + struct PsiGateOp* op = append_op(c); op->kind = PSI_GATE_CUSTOM; op->custom = owned; op->custom_targets = owned_targets; |
