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

Circuit cutting: add interface integration tests #2231

Merged
merged 11 commits into from
Feb 28, 2022
Merged

Conversation

trbromley
Copy link
Contributor

@trbromley trbromley commented Feb 25, 2022

Context:

Adds tests for the cut_circuit transform that ensures differentiability with all of the interfaces.

@trbromley
Copy link
Contributor Author

[sc-14724]

@codecov
Copy link

codecov bot commented Feb 25, 2022

Codecov Report

Merging #2231 (fe71c95) into master (c4b18f0) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2231   +/-   ##
=======================================
  Coverage   99.26%   99.26%           
=======================================
  Files         231      231           
  Lines       18349    18349           
=======================================
  Hits        18215    18215           
  Misses        134      134           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c4b18f0...fe71c95. Read the comment docs.

@trbromley trbromley changed the title Circuit cutting: add integration tests Circuit cutting: add interface integration tests Feb 25, 2022
@trbromley trbromley marked this pull request as ready for review February 25, 2022 20:38
@trbromley trbromley assigned angusjlowe and unassigned angusjlowe Feb 25, 2022
Copy link
Contributor

@anthayes92 anthayes92 left a comment

Choose a reason for hiding this comment

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

Thanks for the interface tests! gradients are looking promising 😁


res = cut_circuit(x)
res_expected = circuit(x)
assert np.isclose(res.detach().numpy(), res_expected.detach().numpy())
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this the detach method being used here?

Why is it needed for comparisons?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you do assert np.isclose(res, res_expected) then PyTorch gives the error:

RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

Could possibly also use torch.isclose.

return qml.expval(qml.PauliZ(wires=[0]))

x = torch.tensor(0.531, requires_grad=True)
cut_circuit = qcut.cut_circuit(circuit, use_opt_einsum=use_opt_einsum)
Copy link
Contributor

Choose a reason for hiding this comment

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

is qcut.cut_circuit preferred over qml.transforms.cut_circuit?

Copy link
Member

Choose a reason for hiding this comment

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

(flyby comment: why not make it top-level, @qml.cut_circuit?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is qcut.cut_circuit preferred over qml.transforms.cut_circuit?

For sure in user-facing documentation we don't want to go with qcut and should instead go through qml.

(flyby comment: why not make it top-level, @qml.cut_circuit?)

Yes good idea, could do! What is the criteria for qml.func() vs qml.transforms.func()? It looks like it's grown a bit organically so far with both choices used.

Copy link
Member

Choose a reason for hiding this comment

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

What is the criteria for qml.func() vs qml.transforms.func()? It looks like it's grown a bit organically so far with both choices used.

There is no criteria at the moment, it is done depending on UI considerations, and 'how important' a particular transform is!

Copy link
Contributor Author

@trbromley trbromley left a comment

Choose a reason for hiding this comment

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


res = cut_circuit(x)
res_expected = circuit(x)
assert np.isclose(res.detach().numpy(), res_expected.detach().numpy())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you do assert np.isclose(res, res_expected) then PyTorch gives the error:

RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

Could possibly also use torch.isclose.

return qml.expval(qml.PauliZ(wires=[0]))

x = torch.tensor(0.531, requires_grad=True)
cut_circuit = qcut.cut_circuit(circuit, use_opt_einsum=use_opt_einsum)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

is qcut.cut_circuit preferred over qml.transforms.cut_circuit?

For sure in user-facing documentation we don't want to go with qcut and should instead go through qml.

(flyby comment: why not make it top-level, @qml.cut_circuit?)

Yes good idea, could do! What is the criteria for qml.func() vs qml.transforms.func()? It looks like it's grown a bit organically so far with both choices used.

@trbromley trbromley merged commit 7ad040b into master Feb 28, 2022
@trbromley trbromley deleted the qcut_integ_tests branch February 28, 2022 14:02
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.

None yet

4 participants