-
Notifications
You must be signed in to change notification settings - Fork 47
Implement automatic qubit management #1788
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
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
205cc2f
init; changelog
paul0403 cecffa7
add core skeleton; add first simple test
paul0403 8a12270
add sample and counts test
paul0403 a1e65be
update lit test for new auto qubit manage mode unit attr
paul0403 8ede25f
update runtime tests for new device init argument
paul0403 655f5d1
remove some prints
paul0403 a135e45
remove "catalyst does not support device with no wires" pytest
paul0403 e87d392
Merge remote-tracking branch 'origin/main' into paul0403/automatic_qu…
paul0403 2133263
clang warning about comparing between signed and unsigned
paul0403 ed635cb
Merge remote-tracking branch 'origin/main' into paul0403/automatic_qu…
paul0403 a7963bf
add tests for non partial measurements
paul0403 328ddcc
update oqd runtime test's device init call
paul0403 5881c98
changelog
paul0403 09db6d5
typo
paul0403 b7aa6ae
Merge remote-tracking branch 'origin/main' into paul0403/automatic_qu…
paul0403 d61f6aa
codefactor
paul0403 6e14e85
add py lit etst
paul0403 c159c44
move mode to RTDevice constructor
paul0403 ce8e78e
apply changelog suggestions
paul0403 c615341
Merge remote-tracking branch 'origin/main' into paul0403/automatic_qu…
paul0403 68291ae
use batch alloc and outline
paul0403 43a72a2
update other relavant methods in RTDevice to include the new auto_qub…
paul0403 2105c20
Merge remote-tracking branch 'origin/main' into paul0403/automatic_qu…
paul0403 1bb541f
add runtime test
paul0403 0c6e45d
Merge remote-tracking branch 'origin/main' into paul0403/automatic_qu…
paul0403 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
frontend/test/pytest/test_automatic_qubit_management.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
# Copyright 2025 Xanadu Quantum Technologies Inc. | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
This file contains tests for automatic qubit management. | ||
Automatic qubit management refers to when the user does not specify the total number of wires | ||
during device initialization. | ||
|
||
Note that, catalyst and pennylane handles device labels differently. For example, when a new label | ||
qml.gate_or_measurement(wires=1000) | ||
is encountered, core pennylane considers "1000" as a pure wire label, and interprets that as | ||
*one* new wire, with the label "1000". However in catalyst we do not associate wires with | ||
arbitrary labels and require wires to be continuous integers from zero, and we would interpret this | ||
as "allocate new wires until we have 1001 wires, and act on wire[1000]". | ||
|
||
In other words, the reference runs of automatic qubit management should be qjit runs with wires | ||
specified during device initialization, instead of non qjit runs. | ||
paul0403 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
import numpy as np | ||
import pennylane as qml | ||
|
||
from catalyst import qjit | ||
|
||
|
||
def test_partial_sample(backend): | ||
""" | ||
Test that a `sample` terminal measurement with wires specified can be executed | ||
correctly with automatic qubit management. | ||
""" | ||
|
||
def circuit(): | ||
qml.RX(0.0, wires=0) | ||
return qml.sample(wires=[0, 2]) | ||
|
||
wires = [4, None] | ||
devices = [qml.device(backend, shots=10, wires=wire) for wire in wires] | ||
ref, observed = (qjit(qml.qnode(dev)(circuit))() for dev in devices) | ||
assert ref.shape == observed.shape | ||
assert np.allclose(ref, observed) | ||
|
||
|
||
def test_partial_counts(backend): | ||
""" | ||
Test that a `counts` terminal measurement with wires specified can be executed | ||
correctly with automatic qubit management. | ||
""" | ||
|
||
def circuit(): | ||
qml.RX(0.0, wires=0) | ||
return qml.counts(wires=[0, 2]) | ||
|
||
wires = [4, None] | ||
devices = [qml.device(backend, shots=10, wires=wire) for wire in wires] | ||
ref, observed = (qjit(qml.qnode(dev)(circuit))() for dev in devices) | ||
assert (ref[i].shape == observed[i].shape for i in (0, 1)) | ||
assert np.allclose(ref, observed) | ||
|
||
|
||
def test_partial_probs(backend): | ||
""" | ||
Test that a `probs` terminal measurement with wires specified can be executed | ||
correctly with automatic qubit management. | ||
""" | ||
|
||
def circuit(): | ||
qml.PauliX(wires=0) | ||
return qml.probs(wires=[0, 2]) | ||
|
||
wires = [4, None] | ||
devices = [qml.device(backend, wires=wire) for wire in wires] | ||
ref, observed = (qjit(qml.qnode(dev)(circuit))() for dev in devices) | ||
assert ref.shape == observed.shape | ||
assert np.allclose(ref, observed) | ||
|
||
|
||
def test_sample(backend): | ||
""" | ||
Test that a `sample` terminal measurement without wires specified can be executed | ||
correctly with automatic qubit management. | ||
""" | ||
|
||
def circuit(): | ||
qml.RX(0.0, wires=3) | ||
return qml.sample() | ||
|
||
wires = [4, None] | ||
devices = [qml.device(backend, shots=10, wires=wire) for wire in wires] | ||
ref, observed = (qjit(qml.qnode(dev)(circuit))() for dev in devices) | ||
assert ref.shape == observed.shape | ||
assert np.allclose(ref, observed) | ||
|
||
|
||
def test_counts(backend): | ||
""" | ||
Test that a `counts` terminal measurement without wires specified can be executed | ||
correctly with automatic qubit management. | ||
""" | ||
|
||
def circuit(): | ||
qml.RX(0.0, wires=3) | ||
return qml.counts() | ||
|
||
wires = [4, None] | ||
devices = [qml.device(backend, shots=10, wires=wire) for wire in wires] | ||
ref, observed = (qjit(qml.qnode(dev)(circuit))() for dev in devices) | ||
assert (ref[i].shape == observed[i].shape for i in (0, 1)) | ||
assert np.allclose(ref, observed) | ||
|
||
|
||
def test_probs(backend): | ||
""" | ||
Test that a `probs` terminal measurement without wires specified can be executed | ||
correctly with automatic qubit management. | ||
""" | ||
|
||
def circuit(): | ||
qml.PauliX(wires=3) | ||
return qml.probs() | ||
|
||
wires = [4, None] | ||
devices = [qml.device(backend, wires=wire) for wire in wires] | ||
ref, observed = (qjit(qml.qnode(dev)(circuit))() for dev in devices) | ||
assert ref.shape == observed.shape | ||
assert np.allclose(ref, observed) | ||
|
||
|
||
def test_state(backend): | ||
""" | ||
Test that a `state` terminal measurement without wires specified can be executed | ||
correctly with automatic qubit management. | ||
""" | ||
|
||
def circuit(): | ||
qml.PauliX(wires=3) | ||
return qml.state() | ||
|
||
wires = [4, None] | ||
devices = [qml.device(backend, wires=wire) for wire in wires] | ||
ref, observed = (qjit(qml.qnode(dev)(circuit))() for dev in devices) | ||
assert ref.shape == observed.shape | ||
assert np.allclose(ref, observed) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.