1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
#include <stdio.h>
#include "psi.h"
int main(void)
{
printf("psi %s\n", psi_version());
struct PsiComplex a = psi_new_complex(1.0, 2.0);
struct PsiComplex b = psi_new_complex(3.0, -1.0);
struct PsiComplex sum = psi_add_complex(a, b);
struct PsiComplex product = psi_mul_complex(a, b);
printf("a = %g + %gi\n", a.real, a.imaginary);
printf("b = %g + %gi\n", b.real, b.imaginary);
printf("a + b = %g + %gi\n", sum.real, sum.imaginary);
printf("a * b = %g + %gi\n", product.real, product.imaginary);
printf("|a| = %g\n", psi_abs_complex(a));
struct PsiVector u = psi_column_vector(
psi_new_complex(1.0, 0.0),
psi_new_complex(2.0, 0.0),
psi_new_complex(3.0, 0.0));
struct PsiVector v = psi_column_vector(
psi_new_complex(4.0, 0.0),
psi_new_complex(5.0, 0.0),
psi_new_complex(6.0, 0.0));
struct PsiComplex dot = psi_dot_vector(u, v);
struct PsiComplex norm = psi_norm_vector(u);
printf("u . v = %g + %gi\n", dot.real, dot.imaginary);
printf("|u| = %g + %gi\n", norm.real, norm.imaginary);
psi_free_vector(&u);
psi_free_vector(&v);
struct PsiMatrix m = psi_matrix(2, 2,
psi_new_complex(1.0, 0.0), psi_new_complex(2.0, 0.0),
psi_new_complex(3.0, 0.0), psi_new_complex(4.0, 0.0));
struct PsiMatrix mm = psi_dot_matrix(m, m);
printf("m*m = [%g %g; %g %g]\n",
psi_get_matrix(mm, 0, 0).real, psi_get_matrix(mm, 0, 1).real,
psi_get_matrix(mm, 1, 0).real, psi_get_matrix(mm, 1, 1).real);
struct PsiVector x = psi_column_vector(
psi_new_complex(1.0, 0.0),
psi_new_complex(1.0, 0.0));
struct PsiVector mx = psi_mul_vector_matrix(x, m);
printf("m*x = [%g, %g]\n", mx.data[0].real, mx.data[1].real);
psi_free_vector(&x);
psi_free_vector(&mx);
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);
const char *names[] = { "q0", "q1" };
struct PsiQuantumRegister reg = psi_new_quantum_register("bell", names, 2);
struct PsiQuantumGate hadamard = psi_hadamard_gate();
struct PsiQuantumGate cnot = psi_cnot_gate();
size_t t0[] = { 0 };
size_t t01[] = { 0, 1 };
psi_apply_gate(®, hadamard, t0, 1);
psi_apply_gate(®, cnot, t01, 2);
printf("bell = [%g, %g, %g, %g]\n",
reg.state_vector.data[0].real, reg.state_vector.data[1].real,
reg.state_vector.data[2].real, reg.state_vector.data[3].real);
psi_free_quantum_gate(&hadamard);
psi_free_quantum_gate(&cnot);
psi_free_quantum_register(®);
struct PsiCompositeGateOp bell_ops[] = {
{ PSI_OP_H, { 0 }, 1 },
{ PSI_OP_CNOT, { 0, 1 }, 2 },
};
struct PsiCustomGate bell = psi_new_custom_gate_from_composite("BELL", 2, bell_ops, 2);
struct PsiQuantumGate bell_gate = psi_to_quantum_gate(bell);
struct PsiQuantumRegister reg2 = psi_new_quantum_register("bell2", names, 2);
size_t t01b[] = { 0, 1 };
psi_apply_gate(®2, bell_gate, t01b, 2);
printf("custom = %s -> [%g, %g, %g, %g]\n", bell_gate.name,
reg2.state_vector.data[0].real, reg2.state_vector.data[1].real,
reg2.state_vector.data[2].real, reg2.state_vector.data[3].real);
psi_free_custom_gate(&bell);
psi_free_quantum_gate(&bell_gate);
psi_free_quantum_register(®2);
const char *cnames[] = { "c0", "c1" };
struct PsiClassicalRegister creg = psi_new_classical_register("c", cnames, 2);
creg.bits[1].state = true;
printf("creg = %s=%d %s=%d\n",
creg.bits[0].name, creg.bits[0].state,
creg.bits[1].name, creg.bits[1].state);
psi_free_classical_register(&creg);
struct PsiQuantumCircuit circuit = psi_new_quantum_circuit(3);
psi_apply_h(&circuit, 0);
psi_apply_cnot(&circuit, 0, 1);
psi_apply_rx(&circuit, 2, 1.5707963267948966);
psi_apply_ccnot(&circuit, 0, 1, 2);
struct PsiCompositeGateOp cops[] = { { PSI_OP_X, { 0 }, 1 } };
struct PsiCustomGate xg = psi_new_custom_gate_from_composite("MYX", 1, cops, 1);
size_t ct[] = { 2 };
psi_apply_custom(&circuit, xg, ct, 1);
psi_measure_all(&circuit);
printf("circuit = %zu qubits, %zu classical, %zu ops\n",
circuit.num_qubits, circuit.num_classical, circuit.operation_count);
for (size_t i = 0; i < circuit.operation_count; i++)
{
size_t tc;
const size_t *targets = psi_gate_op_quantum_targets(&circuit.operations[i], &tc);
printf(" %zu: %s on", i, psi_gate_op_name(circuit.operations[i]));
for (size_t j = 0; j < tc; j++)
printf(" q%zu", targets[j]);
printf("\n");
}
psi_free_quantum_circuit(&circuit);
size_t kt[] = { 0 };
struct PsiKernelBatch batch = psi_new_kernel_batch(1);
struct PsiQuantumGate hg = psi_hadamard_gate();
psi_add_kernel(&batch, psi_new_kernel("H", psi_clone_matrix(hg.matrix), kt, 1));
psi_add_kernel(&batch, psi_new_kernel("H", psi_clone_matrix(hg.matrix), kt, 1));
psi_free_quantum_gate(&hg);
size_t before = batch.count;
psi_optimize_kernel_batch(&batch);
struct PsiVector state = psi_new_state_0();
psi_execute_kernel_batch(batch, &state);
printf("kernels = %zu -> %zu (fused), HH|0> = [%g, %g]\n",
before, batch.count, state.data[0].real, state.data[1].real);
psi_free_vector(&state);
psi_free_kernel_batch(&batch);
}
|