File size: 5,971 Bytes
9425aed | 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | ! =====================================================================
! TEST: Phase 1 - Circuit Depth Calculation
! =====================================================================
! Tests logical_depth() and gate_count() methods
! Reference: SCAFFOLDS_IN_PROGRESS.md, Task 2.1
! =====================================================================
program test_phase1_depth
use bob_circuit
use bob_kinds
implicit none
type(bob_circuit_t) :: c
integer(i4) :: depth, gate_cnt, st
integer :: passed, failed, test_num
passed = 0
failed = 0
test_num = 0
print *, "========================================================"
print *, "PHASE 1 CIRCUIT DEPTH TESTS"
print *, "========================================================"
print *, ""
! ===================================================================
! TEST 1: Single Hadamard (depth should be 1)
! ===================================================================
test_num = 1
print *, "TEST ", test_num, ": Single Hadamard gate"
c = circuit_new(1, 0)
call c%add_gate(GATE_HADAMARD, 0, status=st)
depth = c%logical_depth()
gate_cnt = c%gate_count()
print *, " Gates: ", gate_cnt, " (expected: 1)"
print *, " Logical depth: ", depth, " (expected: 1)"
if (gate_cnt == 1 .and. depth == 1) then
print *, " β PASSED"
passed = passed + 1
else
print *, " β FAILED"
failed = failed + 1
end if
print *, ""
! ===================================================================
! TEST 2: Two parallel Hadamards on different qubits (depth should be 1)
! ===================================================================
test_num = 2
print *, "TEST ", test_num, ": Two parallel Hadamards (different qubits)"
c = circuit_new(2, 0)
call c%add_gate(GATE_HADAMARD, 0, status=st)
call c%add_gate(GATE_HADAMARD, 1, status=st)
depth = c%logical_depth()
gate_cnt = c%gate_count()
print *, " Gates: ", gate_cnt, " (expected: 2)"
print *, " Logical depth: ", depth, " (expected: 1) [parallel gates]"
if (gate_cnt == 2 .and. depth == 1) then
print *, " β PASSED"
passed = passed + 1
else
print *, " β FAILED (parallel gates should have depth 1)"
failed = failed + 1
end if
print *, ""
! ===================================================================
! TEST 3: Serial gates on same qubit (depth should be 3)
! ===================================================================
test_num = 3
print *, "TEST ", test_num, ": Three serial gates on same qubit"
c = circuit_new(1, 0)
call c%add_gate(GATE_HADAMARD, 0, status=st)
call c%add_gate(GATE_PAULI_X, 0, status=st)
call c%add_gate(GATE_PAULI_Z, 0, status=st)
depth = c%logical_depth()
gate_cnt = c%gate_count()
print *, " Gates: ", gate_cnt, " (expected: 3)"
print *, " Logical depth: ", depth, " (expected: 3) [serial gates]"
if (gate_cnt == 3 .and. depth == 3) then
print *, " β PASSED"
passed = passed + 1
else
print *, " β FAILED"
failed = failed + 1
end if
print *, ""
! ===================================================================
! TEST 4: CNOT creates dependency (depth should be 2)
! ===================================================================
test_num = 4
print *, "TEST ", test_num, ": CNOT with dependency"
c = circuit_new(2, 0)
call c%add_gate(GATE_HADAMARD, 0, status=st)
call c%add_gate(GATE_HADAMARD, 1, status=st) ! Parallel H
call c%add_gate(GATE_CNOT, 1, control=0, status=st) ! Depends on both q0 and q1
depth = c%logical_depth()
gate_cnt = c%gate_count()
print *, " Gates: ", gate_cnt, " (expected: 3)"
print *, " Logical depth: ", depth, " (expected: 2) [H layer + CNOT layer]"
if (gate_cnt == 3 .and. depth == 2) then
print *, " β PASSED"
passed = passed + 1
else
print *, " β FAILED"
failed = failed + 1
end if
print *, ""
! ===================================================================
! TEST 5: Empty circuit (depth should be 0)
! ===================================================================
test_num = 5
print *, "TEST ", test_num, ": Empty circuit"
c = circuit_new(2, 0)
depth = c%logical_depth()
gate_cnt = c%gate_count()
print *, " Gates: ", gate_cnt, " (expected: 0)"
print *, " Logical depth: ", depth, " (expected: 0)"
if (gate_cnt == 0 .and. depth == 0) then
print *, " β PASSED"
passed = passed + 1
else
print *, " β FAILED"
failed = failed + 1
end if
print *, ""
! ===================================================================
! TEST 6: Bell state circuit
! ===================================================================
test_num = 6
print *, "TEST ", test_num, ": Bell pair (H + CNOT)"
c = circuit_bell_pair()
depth = c%logical_depth()
gate_cnt = c%gate_count()
print *, " Gates: ", gate_cnt
print *, " Logical depth: ", depth, " (expected: 2) [H then CNOT]"
! Bell pair: H on q0, CNOT(q1, control=q0), measure on q0, measure on q1
! Depth should be: H=1, CNOT=2 (depends on H), measure=3 (both)
! So max is 3 for full circuit, but if we count just H+CNOT it's 2
if (gate_cnt >= 2 .and. depth >= 2) then
print *, " β PASSED (depth >= 2)"
passed = passed + 1
else
print *, " β FAILED"
failed = failed + 1
end if
print *, ""
! ===================================================================
! Summary
! ===================================================================
print *, "========================================================"
print *, "SUMMARY"
print *, " Passed: ", passed, "/", test_num
print *, " Failed: ", failed, "/", test_num
print *, "========================================================"
if (failed == 0) then
print *, "ALL TESTS PASSED β"
stop 0
else
print *, "SOME TESTS FAILED β"
stop 1
end if
end program test_phase1_depth
|