Skip to content

Commit 487b0db

Browse files
authored
Merge pull request #13 from QuantumBFS/formatting
Better handling of optional arguments
2 parents 1484976 + 7b448d4 commit 487b0db

File tree

7 files changed

+367
-240
lines changed

7 files changed

+367
-240
lines changed

Project.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ authors = ["Arsh Sharma <sharmarsh15@gmail.com> and contributors"]
44
version = "0.1.0"
55

66
[deps]
7+
Configurations = "5218b696-f38b-4ac9-8b61-a12ec717816d"
78
IBMQClient = "02860bad-daa6-4506-8736-4a3616c18085"
8-
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
9+
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
10+
Yao = "5872b779-8223-5990-8dd0-5abbb0748c8c"
911
YaoBlocks = "418bc28f-b43b-5e0b-a6e7-61bbc1a2c1df"
1012

1113
[compat]
@@ -15,7 +17,6 @@ julia = "1.6"
1517

1618
[extras]
1719
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
18-
Yao = "5872b779-8223-5990-8dd0-5abbb0748c8c"
1920

2021
[targets]
21-
test = ["Test", "Yao"]
22+
test = ["Test"]

docs/make.jl

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,16 @@ indigo = DocThemeIndigo.install(YaoBlocksQobj)
44

55

66
makedocs(;
7-
modules=[YaoBlocksQobj],
8-
authors="Arsh Sharma <sharmarsh15@gmail.com> and contributors",
9-
repo="https://github.com/QuantumBFS/YaoBlocksQobj.jl/blob/{commit}{path}#{line}",
10-
sitename="YaoBlocksQobj.jl",
11-
format=Documenter.HTML(;
12-
prettyurls=get(ENV, "CI", "false") == "true",
13-
canonical="https://QuantumBFS.github.io/YaoBlocksQobj.jl",
14-
assets=String[indigo, "assets/default.css"],
7+
modules = [YaoBlocksQobj],
8+
authors = "Arsh Sharma <sharmarsh15@gmail.com> and contributors",
9+
repo = "https://github.com/QuantumBFS/YaoBlocksQobj.jl/blob/{commit}{path}#{line}",
10+
sitename = "YaoBlocksQobj.jl",
11+
format = Documenter.HTML(;
12+
prettyurls = get(ENV, "CI", "false") == "true",
13+
canonical = "https://QuantumBFS.github.io/YaoBlocksQobj.jl",
14+
assets = String[indigo, "assets/default.css"],
1515
),
16-
pages=[
17-
"Quickstart" => "index.md",
18-
"API References" => "refs.md",
19-
],
16+
pages = ["Quickstart" => "index.md", "API References" => "refs.md"],
2017
)
2118

22-
deploydocs(;
23-
repo="github.com/QuantumBFS/YaoBlocksQobj.jl",devbranch = "main",
24-
)
19+
deploydocs(; repo = "github.com/QuantumBFS/YaoBlocksQobj.jl", devbranch = "main")

src/YaoBlocksQobj.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
module YaoBlocksQobj
22

3-
export create_qobj
3+
export convert_to_qobj, convert_to_qbir
44

5+
using Configurations
56
using IBMQClient.Schema
67
using YaoBlocks
78

89
include("qobj.jl")
10+
include("qbir.jl")
911

1012
end

src/qbir.jl

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
using Yao
2+
3+
mutable struct U1{T<:Number} <: PrimitiveBlock{1}
4+
lambda::T
5+
end
6+
7+
# 2-parameter 1-pulse single qubit gate
8+
mutable struct U2{T<:Number} <: PrimitiveBlock{1}
9+
phi::T
10+
lambda::T
11+
end
12+
13+
# 3-parameter 2-pulse single qubit gate
14+
mutable struct U3{T<:Number} <: PrimitiveBlock{1}
15+
theta::T
16+
phi::T
17+
lambda::T
18+
end
19+
20+
Base.:(==)(u1_1::U1, u1_2::U1) = u1_1.lambda == u1_2.lambda
21+
Base.:(==)(u2_1::U2, u2_2::U2) = u2_1.phi == u2_2.phi && u2_1.lambda == u2_2.lambda
22+
Base.:(==)(u3_1::U3, u3_2::U3) =
23+
u3_1.theta == u3_2.theta && u3_1.phi == u3_2.phi && u3_1.lambda == u3_2.lambda
24+
25+
function Yao.mat(::Type{T}, u1::U1) where {T}
26+
λ = u1.lambda
27+
28+
T[
29+
1 0
30+
0 exp(im * λ)
31+
]
32+
end
33+
34+
function Yao.mat(::Type{T}, u2::U2) where {T}
35+
ϕ, λ = u2.phi, u2.lambda
36+
37+
T[
38+
1/√2 (-exp(im * λ))/√2
39+
(exp(im * ϕ))/√2 (exp(im *+ λ)))/√2
40+
]
41+
end
42+
43+
function Yao.mat(::Type{T}, u3::U3) where {T}
44+
θ, ϕ, λ = u3.theta, u3.phi, u3.lambda
45+
46+
T[
47+
cos/ 2) -sin/ 2)*exp(im * λ)
48+
sin/ 2)*exp(im * ϕ) cos/ 2)*exp(im *+ λ))
49+
]
50+
end
51+
52+
Yao.iparams_eltype(::U1{T}) where {T} = T
53+
Yao.iparams_eltype(::U2{T}) where {T} = T
54+
Yao.iparams_eltype(::U3{T}) where {T} = T
55+
56+
Yao.getiparams(u1::U1{T}) where {T} = (u1.lambda)
57+
Yao.getiparams(u2::U2{T}) where {T} = (u2.phi, u2.lambda)
58+
Yao.getiparams(u3::U3{T}) where {T} = (u3.theta, u3.phi, u3.lambda)
59+
60+
function Yao.setiparams!(u1::U1{T}, λ) where {T}
61+
u1.lambda = λ
62+
return u1
63+
end
64+
65+
function Yao.setiparams!(u2::U2{T}, ϕ, λ) where {T}
66+
u2.phi = ϕ
67+
u2.lambda = λ
68+
return u2
69+
end
70+
71+
function Yao.setiparams!(u3::U3{T}, θ, ϕ, λ) where {T}
72+
u3.theta = θ
73+
u3.phi = ϕ
74+
u3.lambda = λ
75+
return u3
76+
end
77+
78+
YaoBlocks.@dumpload_fallback U1 U1
79+
YaoBlocks.@dumpload_fallback U2 U2
80+
YaoBlocks.@dumpload_fallback U3 U3
81+
82+
"""
83+
convert_to_qbir(inst)
84+
85+
Converts Qobj based instructions back to YaoIR.
86+
87+
- `inst`: The Qobj based instructions.
88+
89+
For Example:
90+
```julia
91+
q = convert_to_qobj(chain(1, put(1 => H)))
92+
ir = q.experiments[1].instructions |> convert_to_qbir
93+
```
94+
"""
95+
function convert_to_qbir(inst)
96+
n = maximum(x -> maximum(x.qubits), inst) + 1
97+
chain(
98+
n,
99+
map(inst) do x
100+
name, locs = x.name, x.qubits .+ 1
101+
nc = 0
102+
while name[nc+1] == 'c' && nc < length(name)
103+
nc += 1
104+
end
105+
if nc > 0
106+
control(
107+
n,
108+
locs[1:nc],
109+
locs[nc+1:end] => name_index(name[nc+1:end], x.params),
110+
)
111+
elseif name == "measure"
112+
put(n, locs => Yao.Measure(locs...))
113+
else
114+
put(n, locs => name_index(name, x.params))
115+
end
116+
end,
117+
)
118+
end
119+
120+
function name_index(name, params = nothing)
121+
if name == "u1"
122+
U1(params...)
123+
elseif name == "u2"
124+
if isapprox(params, [0, π])
125+
H
126+
else
127+
U2(params...)
128+
end
129+
elseif name == "u3"
130+
if isapprox(params[2], -π / 2) && isapprox(params[3], π / 2)
131+
Rx(params[1])
132+
elseif params[2] == 0 && params[3] == 0
133+
Ry(params[1])
134+
else
135+
U3(params...)
136+
end
137+
elseif name == "id"
138+
I2
139+
elseif name == "x"
140+
X
141+
elseif name == "y"
142+
Y
143+
elseif name == "z"
144+
Z
145+
elseif name == "t"
146+
T
147+
elseif name == "swap"
148+
SWAP
149+
else
150+
error("gate type `$name` not defined!")
151+
end
152+
end

0 commit comments

Comments
 (0)