|
1 | 1 | # Circuit Interface Implementation for ZXCircuit |
2 | 2 |
|
3 | | -nqubits(circ::ZXCircuit) = max(length(circ.inputs), length(circ.outputs)) |
| 3 | +nqubits(circ::ZXCircuit) = circ.layout.nbits |
4 | 4 | get_inputs(circ::ZXCircuit) = circ.inputs |
5 | 5 | get_outputs(circ::ZXCircuit) = circ.outputs |
6 | 6 |
|
7 | | -# Gate operations - these will be defined in gate_ops.jl for ZXDiagram |
8 | | -# but ZXCircuit doesn't implement them directly yet |
9 | | -# For now, we define stubs that throw NotImplementedError |
| 7 | +# Gate operations for ZXCircuit |
| 8 | +# These operate on the underlying ZXGraph |
10 | 9 |
|
11 | | -function push_gate!(circ::ZXCircuit, args...) |
12 | | - error("push_gate! not yet implemented for ZXCircuit. Use ZXDiagram for gate operations.") |
| 10 | +function push_gate!(circ::ZXCircuit{T, P}, ::Val{:Z}, loc::T, phase=zero(P); autoconvert::Bool=true) where {T, P} |
| 11 | + @inbounds out_id = get_outputs(circ)[loc] |
| 12 | + @inbounds bound_id = neighbors(circ, out_id)[1] |
| 13 | + rphase = autoconvert ? safe_convert(P, phase) : phase |
| 14 | + insert_spider!(circ, bound_id, out_id, SpiderType.Z, rphase) |
| 15 | + return circ |
13 | 16 | end |
14 | 17 |
|
15 | | -function pushfirst_gate!(circ::ZXCircuit, args...) |
16 | | - error("pushfirst_gate! not yet implemented for ZXCircuit. Use ZXDiagram for gate operations.") |
| 18 | +function push_gate!(circ::ZXCircuit{T, P}, ::Val{:X}, loc::T, phase=zero(P); autoconvert::Bool=true) where {T, P} |
| 19 | + @inbounds out_id = get_outputs(circ)[loc] |
| 20 | + @inbounds bound_id = neighbors(circ, out_id)[1] |
| 21 | + rphase = autoconvert ? safe_convert(P, phase) : phase |
| 22 | + insert_spider!(circ, bound_id, out_id, SpiderType.X, rphase) |
| 23 | + return circ |
| 24 | +end |
| 25 | + |
| 26 | +function push_gate!(circ::ZXCircuit{T, P}, ::Val{:H}, loc::T) where {T, P} |
| 27 | + @inbounds out_id = get_outputs(circ)[loc] |
| 28 | + @inbounds bound_id = neighbors(circ, out_id)[1] |
| 29 | + insert_spider!(circ, bound_id, out_id, SpiderType.H) |
| 30 | + return circ |
| 31 | +end |
| 32 | + |
| 33 | +function push_gate!(circ::ZXCircuit{T, P}, ::Val{:SWAP}, locs::Vector{T}) where {T, P} |
| 34 | + q1, q2 = locs |
| 35 | + push_gate!(circ, Val{:Z}(), q1) |
| 36 | + push_gate!(circ, Val{:Z}(), q2) |
| 37 | + push_gate!(circ, Val{:Z}(), q1) |
| 38 | + push_gate!(circ, Val{:Z}(), q2) |
| 39 | + v1, v2, bound_id1, bound_id2 = (sort!(spiders(circ)))[(end - 3):end] |
| 40 | + rem_edge!(circ, v1, bound_id1) |
| 41 | + rem_edge!(circ, v2, bound_id2) |
| 42 | + add_edge!(circ, v1, bound_id2) |
| 43 | + add_edge!(circ, v2, bound_id1) |
| 44 | + return circ |
| 45 | +end |
| 46 | + |
| 47 | +function push_gate!(circ::ZXCircuit{T, P}, ::Val{:CNOT}, loc::T, ctrl::T) where {T, P} |
| 48 | + push_gate!(circ, Val{:Z}(), ctrl) |
| 49 | + push_gate!(circ, Val{:X}(), loc) |
| 50 | + @inbounds v1, v2 = (sort!(spiders(circ)))[(end - 1):end] |
| 51 | + add_edge!(circ, v1, v2) |
| 52 | + add_power!(circ, 1) |
| 53 | + return circ |
| 54 | +end |
| 55 | + |
| 56 | +function push_gate!(circ::ZXCircuit{T, P}, ::Val{:CZ}, loc::T, ctrl::T) where {T, P} |
| 57 | + push_gate!(circ, Val{:Z}(), ctrl) |
| 58 | + push_gate!(circ, Val{:Z}(), loc) |
| 59 | + @inbounds v1, v2 = (sort!(spiders(circ)))[(end - 1):end] |
| 60 | + add_edge!(circ, v1, v2) |
| 61 | + insert_spider!(circ, v1, v2, SpiderType.H) |
| 62 | + add_power!(circ, 1) |
| 63 | + return circ |
| 64 | +end |
| 65 | + |
| 66 | +function pushfirst_gate!(circ::ZXCircuit{T, P}, ::Val{:Z}, loc::T, phase::P=zero(P)) where {T, P} |
| 67 | + @inbounds in_id = get_inputs(circ)[loc] |
| 68 | + @inbounds bound_id = neighbors(circ, in_id)[1] |
| 69 | + insert_spider!(circ, in_id, bound_id, SpiderType.Z, phase) |
| 70 | + return circ |
| 71 | +end |
| 72 | + |
| 73 | +function pushfirst_gate!(circ::ZXCircuit{T, P}, ::Val{:X}, loc::T, phase::P=zero(P)) where {T, P} |
| 74 | + @inbounds in_id = get_inputs(circ)[loc] |
| 75 | + @inbounds bound_id = neighbors(circ, in_id)[1] |
| 76 | + insert_spider!(circ, in_id, bound_id, SpiderType.X, phase) |
| 77 | + return circ |
| 78 | +end |
| 79 | + |
| 80 | +function pushfirst_gate!(circ::ZXCircuit{T, P}, ::Val{:H}, loc::T) where {T, P} |
| 81 | + @inbounds in_id = get_inputs(circ)[loc] |
| 82 | + @inbounds bound_id = neighbors(circ, in_id)[1] |
| 83 | + insert_spider!(circ, in_id, bound_id, SpiderType.H) |
| 84 | + return circ |
| 85 | +end |
| 86 | + |
| 87 | +function pushfirst_gate!(circ::ZXCircuit{T, P}, ::Val{:SWAP}, locs::Vector{T}) where {T, P} |
| 88 | + q1, q2 = locs |
| 89 | + pushfirst_gate!(circ, Val{:Z}(), q1) |
| 90 | + pushfirst_gate!(circ, Val{:Z}(), q2) |
| 91 | + pushfirst_gate!(circ, Val{:Z}(), q1) |
| 92 | + pushfirst_gate!(circ, Val{:Z}(), q2) |
| 93 | + v1, v2, bound_id1, bound_id2 = (sort!(spiders(circ)))[1:4] |
| 94 | + rem_edge!(circ, v1, bound_id1) |
| 95 | + rem_edge!(circ, v2, bound_id2) |
| 96 | + add_edge!(circ, v1, bound_id2) |
| 97 | + add_edge!(circ, v2, bound_id1) |
| 98 | + return circ |
| 99 | +end |
| 100 | + |
| 101 | +function pushfirst_gate!(circ::ZXCircuit{T, P}, ::Val{:CNOT}, loc::T, ctrl::T) where {T, P} |
| 102 | + pushfirst_gate!(circ, Val{:X}(), loc) |
| 103 | + pushfirst_gate!(circ, Val{:Z}(), ctrl) |
| 104 | + @inbounds v1, v2 = (sort!(spiders(circ)))[1:2] |
| 105 | + add_edge!(circ, v1, v2) |
| 106 | + add_power!(circ, 1) |
| 107 | + return circ |
| 108 | +end |
| 109 | + |
| 110 | +function pushfirst_gate!(circ::ZXCircuit{T, P}, ::Val{:CZ}, loc::T, ctrl::T) where {T, P} |
| 111 | + pushfirst_gate!(circ, Val{:Z}(), loc) |
| 112 | + pushfirst_gate!(circ, Val{:Z}(), ctrl) |
| 113 | + @inbounds v1, v2 = (sort!(spiders(circ)))[1:2] |
| 114 | + add_edge!(circ, v1, v2) |
| 115 | + insert_spider!(circ, v1, v2, SpiderType.H) |
| 116 | + add_power!(circ, 1) |
| 117 | + return circ |
17 | 118 | end |
0 commit comments