aboutsummaryrefslogtreecommitdiff
path: root/tester/kernels.c
blob: 91a5cba17577618b43a1e309123ff9f2b596cd40 (plain)
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
#include "tests.h"

static const double R2 = 0.7071067811865476;

static void build_fusion(struct PsiQuantumCircuit* c)
{
	psi_apply_h(c, 0);
	psi_apply_z(c, 0);
	psi_apply_h(c, 0);
	psi_apply_x(c, 1);
	psi_apply_x(c, 1);
}

void run_kernel_tests(void)
{
	psi_test_section("Kernel batching and fusion");

	struct PsiQuantumCircuit hh = psi_new_quantum_circuit(1);
	psi_apply_h(&hh, 0);
	psi_apply_h(&hh, 0);
	struct PsiComplex hh_exp[] = { psi_new_complex(1.0, 0.0), psi_new_complex(0.0, 0.0) };
	psi_check_circuit("H then H = identity", &hh, hh_exp, 2);
	psi_free_quantum_circuit(&hh);

	struct PsiKernel a =
			psi_new_kernel("H",
			               psi_matrix(2, 2, psi_new_complex(R2, 0.0), psi_new_complex(R2, 0.0),
			                          psi_new_complex(R2, 0.0), psi_new_complex(-R2, 0.0)),
			               (size_t[]){ 0 }, 1);
	struct PsiKernel b = psi_clone_kernel(a);
	psi_test_check(psi_kernels_can_fuse(a, b), "adjacent single-qubit kernels fuse");
	psi_free_kernel(&a);
	psi_free_kernel(&b);

	struct PsiKernelBatch batch = psi_new_kernel_batch(1);
	psi_add_kernel(
			&batch,
			psi_new_kernel("H",
			               psi_matrix(2, 2, psi_new_complex(R2, 0.0), psi_new_complex(R2, 0.0),
			                          psi_new_complex(R2, 0.0), psi_new_complex(-R2, 0.0)),
			               (size_t[]){ 0 }, 1));
	psi_add_kernel(
			&batch,
			psi_new_kernel("H",
			               psi_matrix(2, 2, psi_new_complex(R2, 0.0), psi_new_complex(R2, 0.0),
			                          psi_new_complex(R2, 0.0), psi_new_complex(-R2, 0.0)),
			               (size_t[]){ 0 }, 1));
	psi_optimize_kernel_batch(&batch);
	psi_test_check(batch.count == 1, "batch fuses two H kernels into one");
	psi_free_kernel_batch(&batch);

	psi_check_runtimes_agree("Runtimes agree on fusion circuit", 2, build_fusion);
}