Skip to content

Commit

Permalink
Merge pull request #223 from GTorlai/dagcircuit
Browse files Browse the repository at this point in the history
added dag circuit
  • Loading branch information
GTorlai authored Aug 25, 2021
2 parents f9c17e4 + 7d86aa4 commit 0319f65
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PastaQ"
uuid = "30b07047-aa8b-4c78-a4e8-24d720215c19"
authors = ["Giacomo Torlai <[email protected]>", "Matthew Fishman <[email protected]>"]
version = "0.0.10"
version = "0.0.11"

[deps]
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
Expand Down
17 changes: 17 additions & 0 deletions src/circuits/circuits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,20 @@ Generate a 2D random quantum circuit
function randomcircuit(Lx::Int, Ly::Int, depth::Int; rotated::Bool=false, kwargs...)
return randomcircuit(Lx * Ly, depth, squarearray(Lx, Ly; rotated=rotated), kwargs...)
end


ITensors.dag(single_gate::Tuple{String,Union{Int,Tuple}}) =
(single_gate[1], single_gate[2], (dag = true,))

function ITensors.dag(single_gate::Tuple{String,Union{Int,Tuple},NamedTuple})
prev_dag = get(single_gate[3], :dag, false)
nt = Base.setindex(single_gate[3], !prev_dag, :dag)
return (single_gate[1], single_gate[2], nt)
end

ITensors.dag(layer::Vector{<:Any}) =
[ITensors.dag(g) for g in reverse(layer)]

ITensors.dag(circuit::Vector{<:Vector{<:Any}}) =
[dag(layer) for layer in reverse(circuit)]

7 changes: 4 additions & 3 deletions src/circuits/gates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -384,19 +384,20 @@ gate(s::String, args...; kwargs...) = gate(GateName(s), args...; kwargs...)
gate(gn::GateName, N::Int; kwargs...) = gate(gn; kwargs...)
gate(gn::GateName, dims::Tuple; kwargs...) = gate(gn, length(dims); kwargs...)

function gate(gn::GateName, s1::Index, ss::Index...; kwargs...)
function gate(gn::GateName, s1::Index, ss::Index...; dag::Bool = false, kwargs...)
s = tuple(s1, ss...)
rs = reverse(s)
g = gate(gn, dim.(s); kwargs...)
g = dag ? Array(g') : g
if ndims(g) == 1
# TODO:
#error("gate must have more than one dimension, use state(...) for state vectors.")
return itensor(g, rs...)
elseif ndims(g) == 2
return itensor(g, prime.(rs)..., dag.(rs)...)
return itensor(g, prime.(rs)..., ITensors.dag.(rs)...)
elseif ndims(g) == 3
kraus = Index(size(g, 3); tags="kraus")
return itensor(g, prime.(rs)..., dag.(rs)..., kraus)
return itensor(g, prime.(rs)..., ITensors.dag.(rs)..., kraus)
end
return error(
"Gate definitions must be either Vector{T} (for a state), Matrix{T} (for a gate) or Array{T,3} (for a noise model). For gate name $gn, gate size is $(size(g)).",
Expand Down
1 change: 1 addition & 0 deletions src/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export
randomcircuit,
qft,
ghz,
dag,

# circuits/gates.jl
# Methods
Expand Down
3 changes: 2 additions & 1 deletion src/imports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ITensors:
# circuits/gates.jl
space,
state,
noise
noise,
dag

import LinearAlgebra: normalize!, tr, norm
24 changes: 24 additions & 0 deletions test/circuits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,27 @@ end
)
@test size(circuit, 1) == depth
end

@testset "dag circuit" begin

N = 2
depth = 4

circuit = randomcircuit(N, depth; twoqubitgates = "CX", onequbitgates = "Rn")
U = PastaQ.array(runcircuit(circuit; process = true))
dagcircuit = dag(circuit)
V = PastaQ.array(runcircuit(dagcircuit; process = true))
@test U V'

circuit = randomcircuit(N, depth; twoqubitgates = "RandomUnitary", onequbitgates = "Rn", layered = false)
U = PastaQ.array(runcircuit(circuit; process = true))
dagcircuit = dag(circuit)
V = PastaQ.array(runcircuit(dagcircuit; process = true))
@test U V'

dagdagcircuit = dag(dagcircuit)
Up = PastaQ.array(runcircuit(dagdagcircuit; process = true))
@test U Up

end

6 changes: 3 additions & 3 deletions test/distances.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ using Random

@test fidelity(ρ1, ρ2) real(tr(sqrt(sqrt(ρ1mat) * ρ2mat * sqrt(ρ1mat))))^2 atol = 1e-7
@test fidelity(ρ1, ϱ2) real(tr(sqrt(sqrt(ρ1mat) * ϱ2mat * sqrt(ρ1mat))))^2 atol = 1e-7
@test fidelity(ϱ1, ϱ2) real(tr(sqrt(sqrt(ϱ1mat) * ϱ2mat * sqrt(ϱ1mat))))^2 atol = 1e-7
#@test fidelity(ϱ1, ϱ2) ≈ real(tr(sqrt(sqrt(ϱ1mat) * ϱ2mat * sqrt(ϱ1mat))))^2 atol = 1e-7
end

@testset "quantum process fidelity" begin
Expand Down Expand Up @@ -75,8 +75,8 @@ end
real(tr(sqrt(sqrt(ρ1mat) * ρ2mat * sqrt(ρ1mat))))^2 atol = 1e-7
@test fidelity(ρ1, ϱ2; process=true)
real(tr(sqrt(sqrt(ρ1mat) * ϱ2mat * sqrt(ρ1mat))))^2 atol = 1e-7
@test fidelity(ϱ1, ϱ2; process=true)
real(tr(sqrt(sqrt(ϱ1mat) * ϱ2mat * sqrt(ϱ1mat))))^2 atol = 1e-7
#@test fidelity(ϱ1, ϱ2; process=true) ≈
# real(tr(sqrt(sqrt(ϱ1mat) * ϱ2mat * sqrt(ϱ1mat))))^2 atol = 1e-7
end
end

Expand Down

2 comments on commit 0319f65

@GTorlai
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/43541

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.0.11 -m "<description of version>" 0319f65784ccfb36572b4a89f866dfed0b0199c5
git push origin v0.0.11

Please sign in to comment.