|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | | - |
| 14 | +import sympy |
15 | 15 | from attrs import frozen |
16 | 16 |
|
17 | 17 | from qualtran import ( |
|
26 | 26 | SoquetT, |
27 | 27 | ) |
28 | 28 | from qualtran._infra.binst_graph_iterators import greedy_topological_sort |
29 | | -from qualtran.bloqs.basic_gates import CNOT |
| 29 | +from qualtran.bloqs.basic_gates import CNOT, IntState, Swap |
30 | 30 | from qualtran.bloqs.bookkeeping import Allocate, Free |
31 | 31 |
|
32 | 32 |
|
@@ -61,3 +61,24 @@ def test_greedy_topological_sort(): |
61 | 61 | BloqInstance(bloq=Free(dtype=QBit()), i=5), |
62 | 62 | RightDangle, |
63 | 63 | ] |
| 64 | + |
| 65 | + |
| 66 | +def test_topo_sort_with_symbolic_registers(): |
| 67 | + # when _priority returns a symbolic value, networkx will try to use |
| 68 | + # it in a comparison and you would get |
| 69 | + # TypeError: cannot determine truth value of Relational. |
| 70 | + |
| 71 | + n = sympy.Symbol('n') |
| 72 | + bb = BloqBuilder() |
| 73 | + |
| 74 | + # This isn't usually a problem for thru-registers since sympy will |
| 75 | + # simplify n-n to zero, which can be compared. Test against a sided |
| 76 | + # symbolic register |
| 77 | + x = bb.add(IntState(5, n)) |
| 78 | + |
| 79 | + y = bb.add_register('y', n) |
| 80 | + x, y = bb.add(Swap(n), x=x, y=y) |
| 81 | + x, y = bb.add(Swap(n), x=x, y=y) |
| 82 | + cbloq = bb.finalize(x=x, y=y) |
| 83 | + res = list(greedy_topological_sort(cbloq._binst_graph)) |
| 84 | + assert len(res) > 0 |
0 commit comments