File size: 11,228 Bytes
5c61046 | 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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | # yao_types.jl β Core Yao.jl block hierarchy + Topological types
module YaoTypes
using LinearAlgebra
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Abstract Block Hierarchy
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
abstract type AbstractBlock end
abstract type CompositeBlock <: AbstractBlock end
struct PrimitiveGate <: AbstractBlock
name::String
params::Vector{Float64}
nqubits::Int
mat::Matrix{ComplexF64}
end
struct ChainBlock <: CompositeBlock
nqubits::Int
blocks::Vector{AbstractBlock}
end
struct KronBlock <: CompositeBlock
nqubits::Int
locs::Vector{Int}
blocks::Vector{AbstractBlock}
end
struct PutBlock <: CompositeBlock
nqubits::Int
locs::Vector{Int}
block::AbstractBlock
end
struct ControlBlock <: CompositeBlock
nqubits::Int
ctrl_locs::Vector{Int}
ctrl_bits::Vector{Int}
block::AbstractBlock
end
struct MeasureBlock <: AbstractBlock
nqubits::Int
locs::Vector{Int}
end
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TOPOLOGICAL TYPES: Braids, Defects, Anyons
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
struct BraidWord
generators::Vector{Int}
edge_indices::Vector{Int}
n_strands::Int
function BraidWord(gens::Vector{Int}, edges::Vector{Int}, n_strands::Int)
@assert length(gens) == length(edges)
new(gens, edges, n_strands)
end
end
BraidWord(n_strands::Int) = BraidWord(Int[], Int[], n_strands)
struct DefectPair
id::String
anyon_type::Symbol
smooth_defect::Tuple{Int,Int}
rough_defect::Tuple{Int,Int}
braid_trajectory::Vector{Tuple{Int,Int}}
end
mutable struct DefectTracker
defects::Dict{String, DefectPair}
fusion_rules::Dict{Tuple{Symbol,Symbol}, Vector{Symbol}}
lattice_size::Tuple{Int,Int}
time_step::Int
function DefectTracker(lattice_size::Tuple{Int,Int}=(20,20))
rules = Dict(
(:fibonacci, :fibonacci) => [:vacuum, :fibonacci],
(:ising, :ising) => [:vacuum, :fermion],
(:toric, :toric) => [:vacuum],
)
new(Dict{String, DefectPair}(), rules, lattice_size, 0)
end
end
struct LatticeSurgeryOp
op_type::Symbol
defect_ids::Vector{String}
basis::Symbol
ancilla_id::Union{String, Nothing}
end
function allocate_defect_pair!(tracker::DefectTracker, id::String, anyon_type::Symbol,
smooth_pos::Tuple{Int,Int}, rough_pos::Tuple{Int,Int})
pair = DefectPair(id, anyon_type, smooth_pos, rough_pos, [smooth_pos, rough_pos])
tracker.defects[id] = pair
return pair
end
function braid_defects!(tracker::DefectTracker, id1::String, id2::String, direction::Int)
d1 = tracker.defects[id1]
d2 = tracker.defects[id2]
new_traj1 = vcat(d1.braid_trajectory, [d2.rough_defect])
new_traj2 = vcat(d2.braid_trajectory, [d1.rough_defect])
tracker.defects[id1] = DefectPair(d1.id, d1.anyon_type, d1.smooth_defect,
d2.rough_defect, new_traj1)
tracker.defects[id2] = DefectPair(d2.id, d2.anyon_type, d2.smooth_defect,
d1.rough_defect, new_traj2)
tracker.time_step += 1
end
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Accessors & Constructors
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
nqubits(b::AbstractBlock) = b.nqubits
nqubits(b::PrimitiveGate) = b.nqubits
const SQRT2 = sqrt(2.0)
const IM = ComplexF64(0, 1)
H() = PrimitiveGate("H", Float64[], 1, ComplexF64[1 1; 1 -1] / SQRT2)
X() = PrimitiveGate("X", Float64[], 1, ComplexF64[0 1; 1 0])
Y() = PrimitiveGate("Y", Float64[], 1, ComplexF64[0 -IM; IM 0])
Z() = PrimitiveGate("Z", Float64[], 1, ComplexF64[1 0; 0 -1])
S() = PrimitiveGate("S", Float64[], 1, ComplexF64[1 0; 0 IM])
Sdg() = PrimitiveGate("Sdg", Float64[], 1, ComplexF64[1 0; 0 -IM])
T() = PrimitiveGate("T", Float64[], 1, ComplexF64[1 0; 0 exp(IM*Ο/4)])
Tdg() = PrimitiveGate("Tdg", Float64[], 1, ComplexF64[1 0; 0 exp(-IM*Ο/4)])
SX() = PrimitiveGate("SX", Float64[], 1, ComplexF64[(1+IM)/2 (1-IM)/2; (1-IM)/2 (1+IM)/2])
Rx(ΞΈ) = PrimitiveGate("Rx", [ΞΈ], 1, ComplexF64[cos(ΞΈ/2) -IM*sin(ΞΈ/2); -IM*sin(ΞΈ/2) cos(ΞΈ/2)])
Ry(ΞΈ) = PrimitiveGate("Ry", [ΞΈ], 1, ComplexF64[cos(ΞΈ/2) -sin(ΞΈ/2); sin(ΞΈ/2) cos(ΞΈ/2)])
Rz(ΞΈ) = PrimitiveGate("Rz", [ΞΈ], 1, ComplexF64[exp(-IM*ΞΈ/2) 0; 0 exp(IM*ΞΈ/2)])
CNOT() = PrimitiveGate("CX", Float64[], 2, ComplexF64[1 0 0 0; 0 1 0 0; 0 0 0 1; 0 0 1 0])
CZ() = PrimitiveGate("CZ", Float64[], 2, ComplexF64[1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 -1])
function chain(nq::Int, blocks::AbstractBlock...)
ChainBlock(nq, collect(blocks))
end
chain(nq::Int, blocks::Vector{<:AbstractBlock}) = ChainBlock(nq, blocks)
function kron(nq::Int, pairs::Pair{Int,<:AbstractBlock}...)
locs = [p.first for p in pairs]
blks = [p.second for p in pairs]
KronBlock(nq, locs, blks)
end
put(nq::Int, locs::Vector{Int}, block::AbstractBlock) = PutBlock(nq, locs, block)
function control(nq::Int, ctrl_locs::Vector{Int}, target::Pair{Int,<:AbstractBlock})
ControlBlock(nq, ctrl_locs, ones(Int, length(ctrl_locs)), PutBlock(nq, [target.first], target.second))
end
control(nq::Int, ctrl_locs::Vector{Int}, block::AbstractBlock) = ControlBlock(nq, ctrl_locs, ones(Int, length(ctrl_locs)), block)
measure(nq::Int, locs::Vector{Int}) = MeasureBlock(nq, locs)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Heron-Native Decomposition
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function decompose_to_heron(block::AbstractBlock)::AbstractBlock
if block isa PrimitiveGate
return _decompose_primitive(block)
elseif block isa ChainBlock
return ChainBlock(block.nqubits, decompose_to_heron.(block.blocks))
elseif block isa KronBlock
return KronBlock(block.nqubits, block.locs, decompose_to_heron.(block.blocks))
elseif block isa PutBlock
return PutBlock(block.nqubits, block.locs, decompose_to_heron(block.block))
elseif block isa ControlBlock
return ControlBlock(block.nqubits, block.ctrl_locs, block.ctrl_bits, decompose_to_heron(block.block))
elseif block isa MeasureBlock
return block
else
error("Unknown block type: $(typeof(block))")
end
end
function _decompose_primitive(g::PrimitiveGate)::AbstractBlock
name = g.name
params = g.params
if name in ("Rz", "SX", "CX", "CNOT")
return g
elseif name == "Ry"
ΞΈ = params[1]
return chain(1, Rz(Ο/2), SX(), Rz(ΞΈ), SX(), Rz(-Ο/2))
elseif name == "Rx"
ΞΈ = params[1]
return chain(1, Rz(-Ο/2), SX(), Rz(ΞΈ), SX(), Rz(Ο/2))
elseif name == "H"
return chain(1, Rz(Ο/2), SX(), Rz(Ο/2), SX(), Rz(Ο/2))
elseif name == "X"
return chain(1, SX(), SX())
elseif name == "Y"
return chain(1, SX(), Rz(Ο), SX())
elseif name == "Z"
return Rz(Ο)
elseif name == "S"
return Rz(Ο/2)
elseif name == "Sdg"
return Rz(-Ο/2)
elseif name == "T"
return Rz(Ο/4)
elseif name == "Tdg"
return Rz(-Ο/4)
elseif name == "CZ"
return g
else
return g
end
end
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Feature Map Parameters
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
struct FeatureMapParams
data::Array{Float64,3}
n_layers::Int
n_qubits::Int
end
function FeatureMapParams(n_layers::Int, n_qubits::Int; init_scale::Float64=0.1)
data = randn(n_layers, n_qubits, 3) * init_scale .+ 1.0
FeatureMapParams(data, n_layers, n_qubits)
end
Base.getindex(p::FeatureMapParams, i...) = p.data[i...]
Base.setindex!(p::FeatureMapParams, v, i...) = (p.data[i...] = v)
Base.size(p::FeatureMapParams) = size(p.data)
Base.length(p::FeatureMapParams) = length(p.data)
Base.eachindex(p::FeatureMapParams) = eachindex(p.data)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Pauli String & Heavy-Hex Topology
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
struct PauliString
paulis::Vector{Char}
end
PauliString(n::Int) = PauliString(rand(['I','X','Y','Z'], n))
const HERON_EDGES_0 = [
(0, 1), (1, 2),
(0, 3), (1, 3), (1, 4), (2, 4), (2, 5),
(3, 4), (4, 5), (5, 6),
(3, 7), (4, 7), (4, 8), (5, 8), (5, 9), (6, 9),
(7, 8), (8, 9)
]
const HERON_EDGE_INDEX = Dict(edge => i for (i, edge) in enumerate(HERON_EDGES_0))
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Exports
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export AbstractBlock, PrimitiveGate, ChainBlock, KronBlock, PutBlock, ControlBlock, MeasureBlock
export nqubits
export H, X, Y, Z, S, Sdg, T, Tdg, SX, Rx, Ry, Rz, CNOT, CZ
export chain, kron, put, control, measure
export decompose_to_heron
export FeatureMapParams, PauliString
export HERON_EDGES_0, HERON_EDGE_INDEX
export BraidWord, DefectPair, DefectTracker, LatticeSurgeryOp
export allocate_defect_pair!, braid_defects!
end # module YaoTypes
|