Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improvements to precompilation and adjustments to new Quasar api #61

Merged
merged 2 commits into from
Dec 27, 2024

Conversation

kshyatt-aws
Copy link
Contributor

Issue #, if available: N/A

Description of changes:

  • Add a few things to precompilation to trigger it for various TensorProduct combinations.
  • Update the pragma parsing to the newer way of doing this in Quasar 0.0.2 and bump dependency version
  • Other small cleanups

Testing done:

  • Unit tests pass locally
  • Benchmarking in Julia and Python shows this improves "time to first simulation" for QFT, QAOA
  • Tests in Python wrapper pass (PR there coming soon)

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

General

Tests

  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have checked that my tests are not configured for a specific region or account (if appropriate)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@kshyatt-aws kshyatt-aws requested a review from a team as a code owner December 26, 2024 18:05
Copy link

codecov bot commented Dec 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.27%. Comparing base (42a9214) to head (633356a).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #61   +/-   ##
=======================================
  Coverage   98.26%   98.27%           
=======================================
  Files          21       21           
  Lines        2189     2197    +8     
=======================================
+ Hits         2151     2159    +8     
  Misses         38       38           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +709 to +716
rx(1) q[0];
prx(0.1, 0.2) q[0];
x q[0];
ry(0.1) q[0];
ry(1) q[0];
y q[0];
rz(0.1) q[0];
rz(1) q[0];
Copy link
Member

Choose a reason for hiding this comment

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

Why add these specific values?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So that the precompilation for integer-typed argument values gets triggered

Comment on lines +766 to +770
#pragma braket result expectation x(q[0]) @ x(q[1])
#pragma braket result expectation z(q[0]) @ z(q[1])
#pragma braket result expectation y(q[0]) @ y(q[1])
#pragma braket result expectation h(q[0]) @ h(q[1])
#pragma braket result expectation i(q[0]) @ i(q[1])
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to handle expectations of tensor products specifically? I see we're already handling tensor products for variance on the next line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the tensor product type is parametrized by the underlying element type

matrix_rep_raw(g::Rz, ϕ) = (θ = ϕ/2.0; return SMatrix{2,2}(exp(-im*θ), 0.0, 0.0, exp(im*θ)))
matrix_rep_raw(g::Rx, ϕ) = ((sθ, cθ) = sincos(ϕ/2.0); return SMatrix{2,2}(cθ, -im*sθ, -im*sθ, cθ))
matrix_rep_raw(g::Ry, ϕ) = ((sθ, cθ) = sincos(ϕ/2.0); return SMatrix{2,2}(complex(cθ), complex(sθ), -complex(sθ), complex(cθ)))
matrix_rep_raw(g::Rz, ϕ)::SMatrix{2,2,ComplexF64} = ((sθ, cθ) = sincos(ϕ/2.0); return SMatrix{2,2}(cθ - im*sθ, 0.0, 0.0, cθ + im*sθ))
Copy link
Member

Choose a reason for hiding this comment

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

Why switch to the trig expression?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mostly for consistency with the above methods and also sincos can be more optimized than exp(im*phi) in some cases

@@ -311,14 +312,11 @@ function apply_gate!(
g_00, g_10, g_01, g_11 = g_matrix
Threads.@threads for chunk_index = 0:n_chunks-1
# my_amps is the group of amplitude generators which this `Task` will process
Copy link
Member

Choose a reason for hiding this comment

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

Need new explanation here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep will add in a future "lots o' explanatory comments" PR

@@ -95,12 +94,12 @@ function Quasar.visit_pragma(v, program_expr)
end

function parse_matrix(tokens::Vector{Tuple{Int64, Int32, Quasar.Token}}, stack, start, qasm)
inner = Quasar.extract_braced_block(tokens, stack, start, qasm)
inner = Quasar.extract_expression(tokens, Quasar.lbracket, Quasar.rbracket, stack, start, qasm)
Copy link
Member

Choose a reason for hiding this comment

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

Why the switch from the more specific to the more general function? Are the delimiter-specific methods going away in the new Quasar.jl?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because extract_braced_block and other delimiter specific methods no longer exist in Quasar 0.0.2

@kshyatt-aws kshyatt-aws merged commit 0261648 into main Dec 27, 2024
14 of 17 checks passed
@kshyatt-aws kshyatt-aws deleted the ksh/quasar branch December 27, 2024 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants