Skip to content

Commit

Permalink
Change: Make Braket.jl an optional dependency with package extension (#…
Browse files Browse the repository at this point in the history
…36)

* change: Remove Braket.jl dependency

* change: More tests

* fix: More cleanup

* fix: lock GIL appropriately for multiple Python specs

* fix: No more QBP hang

* fix: Working with Python multithreading

* test docs in runtests, use GIL lock

* fix: Remove extranous verify

* fix: Make reset a no-op, close #37
  • Loading branch information
kshyatt-aws authored Aug 19, 2024
1 parent 83850ed commit 89be354
Show file tree
Hide file tree
Showing 55 changed files with 3,910 additions and 2,155 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ jobs:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: julia-actions/cache@v2
- name: Update registry
shell: julia --project=. --color=yes {0}
run: |
using Pkg
Pkg.Registry.update()
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
Expand All @@ -65,6 +70,7 @@ jobs:
shell: julia --project=docs --color=yes {0}
run: |
using Pkg
Pkg.Registry.update()
Pkg.develop(PackageSpec(path=pwd()))
Pkg.instantiate()
- uses: julia-actions/julia-buildpkg@v1
Expand All @@ -75,6 +81,6 @@ jobs:
shell: julia --project=docs --color=yes {0}
run: |
using Documenter: DocMeta, doctest
using Braket, BraketSimulator
DocMeta.setdocmeta!(BraketSimulator, :DocTestSetup, :(using Braket, BraketSimulator); recursive=true)
using BraketSimulator
DocMeta.setdocmeta!(BraketSimulator, :DocTestSetup, :(using BraketSimulator, BraketSimulator.Observables; using BraketSimulator: Program, Circuit, qubits, CNot, H, Rx, FreeParameter, QubitSet, AdjointGradient, BitFlip, qubit_count, Qubit, StateVector, Measure, Probability, Ry, Amplitude, Instruction, DensityMatrix, add_instruction!); recursive=true)
doctest(BraketSimulator)
29 changes: 19 additions & 10 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,56 +1,65 @@
name = "BraketSimulator"
uuid = "76d27892-9a0b-406c-98e4-7c178e9b3dff"
authors = ["Katharine Hyatt <[email protected]> and contributors"]
version = "0.0.1"
version = "0.0.2"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Automa = "67c07d97-cdcb-5c2c-af73-a7f9c32a568b"
Braket = "19504a0f-b47d-4348-9127-acc6cc69ef67"
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
StructTypes = "856f2bd8-1eba-4b0a-8007-ebc267875bd4"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[weakdeps]
Braket = "19504a0f-b47d-4348-9127-acc6cc69ef67"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"

[extensions]
BraketSimulatorPythonExt = "PythonCall"
BraketSimulatorBraketExt = "Braket"
BraketSimulatorPythonExt = ["JSON3", "PythonCall"]

[compat]
AbstractTrees = "=0.4.5"
Aqua = "=0.8"
Automa = "=1.0.3"
Braket = "=0.9.1"
Automa = "=1.0.4"
Braket = "=0.9.4"
Combinatorics = "=1.0.2"
DataStructures = "=0.18.20"
Dates = "1.6"
Documenter = "=1.5.0"
InteractiveUtils = "1.6"
JSON3 = "=1.14.0"
LinearAlgebra = "1.6"
Logging = "1.6"
Pkg = "1.6"
OrderedCollections = "=1.6.3"
PrecompileTools = "=1.2.1"
PythonCall = "=0.9.20"
PythonCall = "=0.9.22"
Random = "1.6"
StaticArrays = "1.9"
StatsBase = "0.34"
StructTypes = "=1.10.0"
Test = "1.6"
UUIDs = "1.6"
julia = "1.9"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Braket = "19504a0f-b47d-4348-9127-acc6cc69ef67"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Aqua", "Test", "Logging", "PythonCall"]
test = ["Aqua", "Braket", "JSON3", "Test", "Logging", "PythonCall"]
8 changes: 8 additions & 0 deletions coverage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Coverage
# process '*.cov' files
coverage = process_folder()
coverage = append!(coverage, process_folder("ext/BraketSimulatorPythonExt/"))
coverage = append!(coverage, process_folder("ext/BraketSimulatorBraketExt/"))
coverage = merge_coverage_counts(coverage)
# Get total coverage for all Julia files
covered_lines, total_lines = get_summary(coverage)
Expand All @@ -11,6 +12,13 @@ open("lcov.info", "w") do io
LCOV.write(io, coverage)
end

for fi in readdir("src")
if endswith(fi, ".jl")
println("Coverage for file $fi")
@show get_summary(process_file(joinpath("src", fi)))
end
end

# uncomment this if you have `genhtml` installed
# to generate HTML coverage info
#run(`genhtml -o .coverage/ lcov.info`)
Expand Down
1 change: 0 additions & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[deps]
Braket = "19504a0f-b47d-4348-9127-acc6cc69ef67"
BraketSimulator = "76d27892-9a0b-406c-98e4-7c178e9b3dff"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
9 changes: 7 additions & 2 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Braket, BraketSimulator
using BraketSimulator
using Documenter

DocMeta.setdocmeta!(BraketSimulator, :DocTestSetup, :(using Braket, BraketSimulator); recursive=true)
DocMeta.setdocmeta!(BraketSimulator, :DocTestSetup, :(using BraketSimulator, BraketSimulator.Observables; using BraketSimulator: Program, Circuit, qubits, CNot, H, Rx, FreeParameter, QubitSet, AdjointGradient, BitFlip, qubit_count, Qubit, StateVector, Measure, Probability, Ry, Amplitude, Instruction, DensityMatrix, add_instruction!); recursive=true)

makedocs(;
modules=[BraketSimulator],
Expand All @@ -15,6 +15,11 @@ makedocs(;
pages=[
"Home" => "index.md",
"Simulators" => "sims.md",
"Gates" => "gates.md",
"Custom Gates" => "custom_gates.md",
"Noises" => "noises.md",
"Observables" => "observables.md",
"Results" => "results.md",
],
)

Expand Down
22 changes: 22 additions & 0 deletions docs/src/circuits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
```@meta
CurrentModule = BraketSimulator
```

# Circuits

Circuits are made up of *instructions* (operations to apply to the qubits -- [gates](gates.md) and [noises](noises.md)) and *result types* ([results](results.md)).
OpenQASM3 programs are parsed to circuits which are then run on the simulator.

```@docs
BraketSimulator.Circuit
BraketSimulator.Operator
BraketSimulator.QuantumOperator
BraketSimulator.FreeParameter
BraketSimulator.Measure
BraketSimulator.Instruction
BraketSimulator.QubitSet
BraketSimulator.Qubit
BraketSimulator.qubit_count
BraketSimulator.qubits
BraketSimulator.basis_rotation_instructions!
```
6 changes: 2 additions & 4 deletions docs/src/custom_gates.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ CurrentModule = BraketSimulator

# Custom gates

`BraketSimulator.jl` defines some custom gates to extend what `Braket.jl` provides. These include the OpenQASM3 built-in gates `gphase` (as `MultiQubitPhaseShift`) and `U` (the single qubit three angle unitary).
`BraketSimulator.jl` defines some custom gates to extend what `Braket.jl` provides.

```@docs
U
MultiQubitPhaseShift
MultiRZ
BraketSimulator.MultiRZ
```
52 changes: 52 additions & 0 deletions docs/src/gates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
```@meta
CurrentModule = BraketSimulator
```

# Gates

`BraketSimulators.jl` provides many pre-implemented gates which can be used to build up circuits. For gates with angle parameters, you can supply `Irrational`s like `π` as arguments.

```@docs
BraketSimulator.Gate
BraketSimulator.AngledGate
BraketSimulator.I
BraketSimulator.X
BraketSimulator.Y
BraketSimulator.Z
BraketSimulator.H
BraketSimulator.Rx
BraketSimulator.Ry
BraketSimulator.Rz
BraketSimulator.V
BraketSimulator.Vi
BraketSimulator.T
BraketSimulator.Ti
BraketSimulator.S
BraketSimulator.Si
BraketSimulator.U
BraketSimulator.Unitary
BraketSimulator.PhaseShift
BraketSimulator.MultiQubitPhaseShift
BraketSimulator.PRx
BraketSimulator.GPi
BraketSimulator.GPi2
BraketSimulator.XX
BraketSimulator.XY
BraketSimulator.YY
BraketSimulator.ZZ
BraketSimulator.ECR
BraketSimulator.MS
BraketSimulator.CPhaseShift
BraketSimulator.CPhaseShift00
BraketSimulator.CPhaseShift01
BraketSimulator.CPhaseShift10
BraketSimulator.CNot
BraketSimulator.CY
BraketSimulator.CZ
BraketSimulator.CV
BraketSimulator.Swap
BraketSimulator.PSwap
BraketSimulator.ISwap
BraketSimulator.CCNot
BraketSimulator.CSwap
```
27 changes: 13 additions & 14 deletions docs/src/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
```@meta
DocTestSetup = quote using Braket, BraketSimulator end
DocTestSetup = quote using BraketSimulator, BraketSimulator.Observables; using BraketSimulator: Program, Circuit, qubits, CNot, H, Rx, FreeParameter, QubitSet, AdjointGradient, BitFlip, qubit_count, Qubit, StateVector, Measure, Probability, Ry, Amplitude, Instruction, DensityMatrix, add_instruction! end
CurrentModule = BraketSimulator
```

Expand Down Expand Up @@ -31,29 +31,28 @@ Pkg.add("BraketSimulator")

Then you can run a simulation of a simple [GHZ state](https://en.wikipedia.org/wiki/Greenberger%E2%80%93Horne%E2%80%93Zeilinger_state) preparation circuit.

!!! note
To simulate OpenQASM3 programs, you will need to load the Python extension `BraketSimulatorPythonExt` like so: `using PythonCall, BraketSimulator`. If
you prefer not to install or use Python, make sure to set the default `IRType` for `Braket.jl` to JAQCD: `Braket.IRType[] = :JAQCD`.

```jldoctest
julia> using Braket, BraketSimulator
julia> using BraketSimulator
julia> using BraketSimulator: Circuit, H, CNot, Amplitude
julia> n_qubits = 10;
julia> c = Circuit();
julia> H(c, 0);
julia> add_instruction!(c, Instruction(H(), 0));
julia> foreach(q->CNot(c, 0, q), 1:n_qubits-1);
julia> foreach(q->add_instruction!(c, Instruction(CNot(), [0, q])), 1:n_qubits-1);
julia> Amplitude(c, [repeat("0", n_qubits), repeat("1", n_qubits)]);
julia> push!(c.result_types, Amplitude([repeat("0", n_qubits), repeat("1", n_qubits)]));
julia> sim = LocalSimulator("braket_sv_v2"); # use the state vector simulator (without noise)
julia> sim = StateVectorSimulator(n_qubits, 0); # use the state vector simulator (without noise)
julia> res = result(simulate(sim, ir(c, Val(:JAQCD)), shots=0));
julia> res = simulate(sim, Program(c), n_qubits, 0);
julia> res.values
1-element Vector{Any}:
Dict{String, ComplexF64}("0000000000" => 0.7071067811865475 + 0.0im, "1111111111" => 0.7071067811865475 + 0.0im)
julia> res.resultTypes[1].value
Dict{String, ComplexF64} with 2 entries:
"0000000000" => 0.707107+0.0im
"1111111111" => 0.707107+0.0im
```

24 changes: 24 additions & 0 deletions docs/src/noises.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
```@meta
CurrentModule = BraketSimulator
```

# Noises

`BraketSimulators.jl` provides many pre-implemented noise channels which can be applied to circuits.
Noisy circuits can be simulated used the density matrix simulator.

```@docs
BraketSimulator.Noise
BraketSimulator.Kraus
BraketSimulator.BitFlip
BraketSimulator.PhaseFlip
BraketSimulator.PauliChannel
BraketSimulator.TwoQubitPauliChannel
BraketSimulator.MultiQubitPauliChannel
BraketSimulator.Depolarizing
BraketSimulator.PhaseDamping
BraketSimulator.AmplitudeDamping
BraketSimulator.GeneralizedAmplitudeDamping
BraketSimulator.TwoQubitDepolarizing
BraketSimulator.TwoQubitDephasing
```
13 changes: 13 additions & 0 deletions docs/src/observables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Observables

```@docs
BraketSimulator.Observables.Observable
BraketSimulator.Observables.X
BraketSimulator.Observables.Y
BraketSimulator.Observables.Z
BraketSimulator.Observables.H
BraketSimulator.Observables.I
BraketSimulator.Observables.TensorProduct
BraketSimulator.Observables.HermitianObservable
BraketSimulator.Observables.Sum
```
14 changes: 14 additions & 0 deletions docs/src/results.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Results


```@docs
BraketSimulator.Result
BraketSimulator.AdjointGradient
BraketSimulator.Expectation
BraketSimulator.Variance
BraketSimulator.Sample
BraketSimulator.Amplitude
BraketSimulator.Probability
BraketSimulator.DensityMatrix
BraketSimulator.StateVector
```
4 changes: 2 additions & 2 deletions docs/src/sims.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CurrentModule = BraketSimulator
Each type is [parameterized](https://docs.julialang.org/en/v1/manual/types/#Parametric-Types) by an **element type** (which should be a Julia `Complex` type, such as `ComplexF64`)
and an **array type** (so that we can specialize for GPU arrays, for example).

Each simulator can be initialized with a `qubit_count` and `shots` value. You may query the [`properties`](@ref Braket.properties) of a simulator to learn what gate types, result types, and other operations it supports.
Each simulator can be initialized with a `qubit_count` and `shots` value. You may query the [`properties`](@ref BraketSimulator.properties) of a simulator to learn what gate types, result types, and other operations it supports.

```@docs
StateVectorSimulator
Expand All @@ -17,5 +17,5 @@ evolve!
simulate
expectation
probabilities
Braket.properties
BraketSimulator.properties
```
Loading

2 comments on commit 89be354

@kshyatt-aws
Copy link
Member 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/113421

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.2 -m "<description of version>" 89be3546e21d17f65b4c0635f7aaec65f39590c5
git push origin v0.0.2

Please sign in to comment.