-
Notifications
You must be signed in to change notification settings - Fork 210
Add collect_runs() function #206
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
10 commits
Select commit
Hold shift + click to select a range
d80bda7
Add collect_runs() function
mtreinish 4c5d8eb
Handle multiple edges to a single successor node
mtreinish be9c548
Add initial tests
mtreinish 47bfa9a
Add more tests
mtreinish 9fe2849
Use Vec.dedup() and graph access instead of Vec of tuples and HashSet
mtreinish 10870a1
Merge branch 'master' into collect-runs
mtreinish 58234a9
Update src/lib.rs
mtreinish 1e716a7
Apply suggestions from code review
mtreinish becd668
Document and test nodes can only exist in a single run
itoko 200e65c
Merge branch 'master' into collect-runs
mtreinish 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| # 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. | ||
|
|
||
| import unittest | ||
|
|
||
| import retworkx | ||
|
|
||
|
|
||
| class TestCollectRuns(unittest.TestCase): | ||
| def test_dagcircuit_basic(self): | ||
| dag = retworkx.PyDAG() | ||
| qr_0_in = dag.add_node("qr[0]") | ||
| qr_0_out = dag.add_node("qr[0]") | ||
| qr_1_in = dag.add_node("qr[1]") | ||
| qr_1_out = dag.add_node("qr[1]") | ||
| cr_0_in = dag.add_node("cr[0]") | ||
| cr_0_out = dag.add_node("cr[0]") | ||
| cr_1_in = dag.add_node("cr[1]") | ||
| cr_1_out = dag.add_node("cr[1]") | ||
|
|
||
| h_gate = dag.add_child(qr_0_in, "h", "qr[0]") | ||
| x_gate = dag.add_child(h_gate, "x", "qr[0]") | ||
| cx_gate = dag.add_child(x_gate, "cx", "qr[0]") | ||
| dag.add_edge(qr_1_in, cx_gate, "qr[1]") | ||
|
|
||
| measure_qr_1 = dag.add_child(cx_gate, "measure", "qr[1]") | ||
| dag.add_edge(cr_1_in, measure_qr_1, "cr[1]") | ||
| x_gate = dag.add_child(measure_qr_1, "x", "qr[1]") | ||
| dag.add_edge(measure_qr_1, x_gate, "cr[1]") | ||
| dag.add_edge(cr_0_in, x_gate, "cr[0]") | ||
|
|
||
| measure_qr_0 = dag.add_child(cx_gate, "measure", "qr[0]") | ||
| dag.add_edge(measure_qr_0, qr_0_out, "qr[0]") | ||
| dag.add_edge(measure_qr_0, cr_0_out, "cr[0]") | ||
| dag.add_edge(x_gate, measure_qr_0, "cr[0]") | ||
|
|
||
| measure_qr_1_out = dag.add_child(x_gate, "measure", "cr[1]") | ||
| dag.add_edge(x_gate, measure_qr_1_out, "qr[1]") | ||
| dag.add_edge(measure_qr_1_out, qr_1_out, "qr[1]") | ||
| dag.add_edge(measure_qr_1_out, cr_1_out, "cr[1]") | ||
|
|
||
| def filter_function(node): | ||
| return node in ['h', 'x'] | ||
|
|
||
| res = retworkx.collect_runs(dag, filter_function) | ||
| expected = [['h', 'x'], ['x']] | ||
| self.assertEqual(expected, res) | ||
|
|
||
| def test_multiple_successor_edges(self): | ||
| dag = retworkx.PyDiGraph() | ||
| q0, q1 = dag.add_nodes_from(['q0', 'q1']) | ||
| cx_1 = dag.add_child(q0, 'cx', 'q0') | ||
| dag.add_edge(q1, cx_1, 'q1') | ||
| cx_2 = dag.add_child(cx_1, 'cx', 'q0') | ||
| dag.add_edge(q1, cx_2, 'q1') | ||
| cx_3 = dag.add_child(cx_2, 'cx', 'q0') | ||
| dag.add_edge(q1, cx_3, 'q1') | ||
|
|
||
| def filter_function(node): | ||
| return node == 'cx' | ||
|
|
||
| res = retworkx.collect_runs(dag, filter_function) | ||
| self.assertEqual([['cx', 'cx', 'cx']], res) | ||
|
|
||
| def test_cycle(self): | ||
| dag = retworkx.PyDiGraph() | ||
| dag.extend_from_edge_list([(0, 1), (1, 2), (2, 0)]) | ||
| with self.assertRaises(retworkx.DAGHasCycle): | ||
| retworkx.collect_runs(dag, lambda _: True) | ||
|
|
||
| def test_filter_function_inner_exception(self): | ||
| dag = retworkx.PyDiGraph() | ||
| dag.add_node('a') | ||
| dag.add_child(0, 'b', None) | ||
|
|
||
| def filter_function(node): | ||
| raise IndexError("Things fail from time to time") | ||
|
|
||
| with self.assertRaises(IndexError): | ||
| retworkx.collect_runs(dag, filter_function) | ||
|
|
||
| def test_empty(self): | ||
| dag = retworkx.PyDAG() | ||
| self.assertEqual([], retworkx.collect_runs(dag, lambda _: True)) | ||
|
|
||
| def test_h_h_cx(self): | ||
| dag = retworkx.PyDiGraph() | ||
| q0, q1 = dag.add_nodes_from(['q0', 'q1']) | ||
| h_1 = dag.add_child(q0, 'h', 'q0') | ||
| h_2 = dag.add_child(q1, 'h', 'q1') | ||
| cx_2 = dag.add_child(h_1, 'cx', 'q0') | ||
| dag.add_edge(h_2, cx_2, 'q1') | ||
|
|
||
| def filter_function(node): | ||
| return node in ['cx', 'h'] | ||
|
|
||
| res = retworkx.collect_runs(dag, filter_function) | ||
| self.assertEqual([['h', 'cx'], ['h']], res) | ||
|
|
||
| def test_cx_h_h_cx(self): | ||
| dag = retworkx.PyDiGraph() | ||
| q0, q1 = dag.add_nodes_from(['q0', 'q1']) | ||
| cx_1 = dag.add_child(q0, 'cx', 'q0') | ||
| dag.add_edge(q1, cx_1, 'q1') | ||
| h_1 = dag.add_child(cx_1, 'h', 'q0') | ||
| h_2 = dag.add_child(cx_1, 'h', 'q1') | ||
| cx_2 = dag.add_child(h_1, 'cx', 'q0') | ||
| dag.add_edge(h_2, cx_2, 'q1') | ||
|
|
||
| def filter_function(node): | ||
| return node in ['cx', 'h'] | ||
|
|
||
| res = retworkx.collect_runs(dag, filter_function) | ||
| self.assertEqual([['cx'], ['h', 'cx'], ['h']], res) | ||
|
|
||
| def test_cx_h_cx(self): | ||
| dag = retworkx.PyDiGraph() | ||
| q0, q1 = dag.add_nodes_from(['q0', 'q1']) | ||
| cx_1 = dag.add_child(q0, 'cx', 'q0') | ||
| dag.add_edge(q1, cx_1, 'q1') | ||
| h_1 = dag.add_child(cx_1, 'h', 'q0') | ||
| cx_2 = dag.add_child(h_1, 'cx', 'q0') | ||
| dag.add_edge(cx_1, cx_2, 'q1') | ||
|
|
||
| def filter_function(node): | ||
| return node in ['cx', 'h'] | ||
|
|
||
| res = retworkx.collect_runs(dag, filter_function) | ||
| self.assertEqual([['cx'], ['h', 'cx']], res) | ||
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.