-
Notifications
You must be signed in to change notification settings - Fork 187
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
Adds tests for decomposed frontend gates #173
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great tests @nquesada! Just some minor comments
.github/CHANGELOG.md
Outdated
@@ -1,3 +1,5 @@ | |||
* Adds new integration tests for the Gaussian gates that are not primitive, i.e. P, CX, CZ, and S2. Addresses issue [#171](https://github.com/XanaduAI/strawberryfields/issues/171) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Adds new integration tests for the Gaussian gates that are not primitive, i.e. P, CX, CZ, and S2. Addresses issue [#171](https://github.com/XanaduAI/strawberryfields/issues/171) | |
* Adds new integration tests for the Gaussian gates that are not primitive, i.e. P, CX, CZ, and S2. Addresses issue | |
[#171](https://github.com/XanaduAI/strawberryfields/issues/171) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, this should be moved down the changelog to within the # Release 0.12.0-dev
section. Probably under a new subsection ### Improvements
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @josh146 . I need to learn some CHANGELOG etiquette :)
Vexpected = 0.5 * hbar * Pmat @ np.diag(np.exp([-2 * r, 2 * r])) @ Pmat.T | ||
assert np.allclose(Vexpected, state.cov()) | ||
rexpected = Pmat @ np.array([x1, p1]) | ||
assert np.allclose(rexpected, state.means()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should atol=tol
and rtol=0
be added to these np.allclose()
calls? Especially since tol
is being included in the test signature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes! Will add it.
r = 3 | ||
x1 = 2 | ||
p1 = 1.3 | ||
s = 0.5 * 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be s = 0.5
? Otherwise it looks like this is just testing that Pgate(0)
acts as the identity
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! Should check again that it works
assert np.allclose(state.cov(), Vexpected, atol=tol) | ||
rexpected = CXmat @ np.array([x1, x2, p1, p2]) | ||
# Checks the means are transformed correctly | ||
assert np.allclose(state.means(), rexpected, atol=tol) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing rtol=0
? By default, I think np.allclose()
included a non-zero relative tolerance
np.exp(1j * phi) * np.sqrt((nbar / (1 + nbar))) | ||
) ** (list(range(n))) | ||
diag_elems[-1] = 0 | ||
assert np.allclose(np.diag(diag_elems), ket, atol=0.2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rtol=0?
assert np.allclose(state.cov(), Vexpected, atol=tol) | ||
rexpected = CZmat @ np.array([x1, x2, p1, p2]) | ||
# Checks the means are transformed correctly | ||
assert np.allclose(state.means(), rexpected, atol=tol) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rtol=0?
eng, prog = setup_eng(N) | ||
nbar = 1 | ||
s = np.arcsinh(np.sqrt(1.0)) | ||
phi = 0 * np.pi / 3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should phi be zero?
n, _ = ket.shape | ||
diag_elems = (1 / np.sqrt(1 + nbar)) * ( | ||
np.exp(1j * phi) * np.sqrt((nbar / (1 + nbar))) | ||
) ** (list(range(n))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused why this line works; the LHS of the ** should be a Python float, while the RHS is a list - how come it evaluates as a NumPy array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, it's because the LHS is of type np.float64
.
Maybe it's worth being a bit explicit, and having the exponent be np.arange(n)
instead of list(range(n))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! Will do. To be honest I was not sure why it worked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the thorough review @josh146 !
…to nonzero values
Codecov Report
@@ Coverage Diff @@
## master #173 +/- ##
=========================================
Coverage ? 97.05%
=========================================
Files ? 43
Lines ? 5398
Branches ? 0
=========================================
Hits ? 5239
Misses ? 159
Partials ? 0 Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #173 +/- ##
=========================================
Coverage ? 97.21%
=========================================
Files ? 40
Lines ? 5273
Branches ? 0
=========================================
Hits ? 5126
Misses ? 147
Partials ? 0
Continue to review full report at Codecov.
|
Co-Authored-By: Josh Izaac <josh146@gmail.com>
Co-Authored-By: Josh Izaac <josh146@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some questions about these tests to resolve before merging
@co9olguy . I agree with Josh. We could add extra tests to check that the gates are applied in the right order by for instance applying the decomposition we have found and then apply the gate backwards and check that effectively we just applied the identity. However, the spirit of these tests was to check that the operations do what they are supposed to do, for example that the P gate takes x-> x and p-> p+ s x. |
@nquesada sounds good. I'm happy to leave these tests, which are more concrete (but less amenable to verifying all backends). We should also add the other suggested abstract test strategy (confirm that gates match their decomposing by reversing the arguments and concatenating) before merging. |
I think this PR could be merged now. We could make an issue to be addressed in a future PR to check that the gates are applied in the right order and that the "compiled" gate applied backwards reverses them. |
I'm concerned that a magic number tolerance might be masking a possible numerical error. I've set tests to run on default tolerance and observed failures even for cutoff as high as 30 dimensions |
Update: cutoff of 35 seems to pass with default tolerance |
@josh146 I'm inclined to drop the failing test, since the numerics are too sensitive. I've added a secondary test for that gate as well, so we can have some confidence |
I'm happy with that, cutoff of 35 is way too high, and the squeezing is almost vacuum to get it to pass with a cutoff of 5. |
@josh146 Ok I removed it. If tests pass, good to merge |
Context:
Several useful Gaussian gates can be decomposed in terms of beamsplitters and single mode squeezing gates. This PR creates tests that explicitly check that these decompositions are correct
Description of the Change:
Adds new test class TestDecompositionsGaussianGates
Benefits:
Explicit testing for the decompositions derived in the documentation
Possible Drawbacks:
Related GitHub Issues:
#171