A draft for the approximate multi-qubit toffoli gate algorithm#1786
A draft for the approximate multi-qubit toffoli gate algorithm#1786chenyizhang2000 wants to merge 5 commits into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
|
The new version has the following updates:
|
mpharrigan
left a comment
There was a problem hiding this comment.
This is fantastic! I think we should name the file approx_multi_toffoli.ipynb or approx_multi_qubit_toffoli.ipynb
The bloq definitions are fully formed and should ultimately get "promoted" to live in a python file in qualtran.bloqs.mcmt. This is a bit more work, so I'm happy to merge the notebook as-is and leave the rest as a follow up; or do it now as part of the PR. The steps would be
- take bloq class definitions and put into
approx_multi_toffoli.py.. import these bloq classes in the concepts notebook - Write one or two
@bloq_exampleexample instantiations https://qualtran.readthedocs.io/en/latest/Autodoc.html#demo-bloqs-with-bloqexample - Add a file for unit tests:
approx_multi_toffoli_test.py. Use the bloq examples to run basic validity checks - add en entry to qualtran_dev_tools/notebookspecs.py to make an autogenerated reference-api notebook. This can stay separate from the "concepts" notebook you've written.
let me know if you'd like to do that or merge as-is
|
Moving the bloq definitions into a python file sounds good! I've made the following updates:
|
|
|
||
| _, _, target = bloq.call_classically(ctrl=int_to_bits(x_int, m)) | ||
|
|
||
| assert target == int(x_int == all_ones) |
There was a problem hiding this comment.
Please add an additional small test that the notebook executes without error. At the top of the file:
import qualtran.testing as qlt_testingand here at the bottom
@pytest.mark.notebook
def test_notebook():
qlt_testing.execute_notebook('approx_multi_toffoli')
Can you say more about this? The unit tests pass on my machine and on the automated CI checks on this PR. You can run the unit tests with |
| qualtran.bloqs.mcmt.approx_multi_toffoli._PARITY_MASK_DOC, | ||
| qualtran.bloqs.mcmt.approx_multi_toffoli._APPROX_MULTI_TOFFOLI_DOC, | ||
| ], | ||
| ), |
There was a problem hiding this comment.
Please run
python dev_tools/autogenerate-bloqs-notebooks-v2.pywhich will create a new notebook "approx_multi_toffoli_reference". You'll need to commit that to git (via git add). It'll also edits docs/bloqs/index.rst to include the new notebook in the table-of-contents. you'll need to commit that too
|
We run the mypy type-checker to try to detect type errors before runtime. You can run this locally with diff --git a/qualtran/bloqs/mcmt/approx_multi_toffoli.py b/qualtran/bloqs/mcmt/approx_multi_toffoli.py
index 6d9e8620..946c7478 100644
--- a/qualtran/bloqs/mcmt/approx_multi_toffoli.py
+++ b/qualtran/bloqs/mcmt/approx_multi_toffoli.py
@@ -23,6 +23,8 @@ from qualtran.drawing import (
directional_text_box,
show_bloq,
)
+from qualtran.simulation.classical_sim import ClassicalValRetT
+
@dataclass(frozen=True)
class MultiAndLogDepth(Bloq):
@@ -190,7 +192,7 @@ class MultiAndLogDepth(Bloq):
junk = [left_out] + left_junk + [right_out] + right_junk
return junk, out
- def on_classical_vals(self, ctrl: np.ndarray) -> Dict[str, np.ndarray]:
+ def on_classical_vals(self, ctrl: np.ndarray) -> Dict[str, ClassicalValRetT]:
ctrl = np.asarray(ctrl, dtype=np.uint8)
effective_ctrl = np.equal(ctrl, np.asarray(self.concrete_cvs)).astype(np.uint8)
junk, target = self._classical_tree(list(effective_ctrl))
@@ -226,7 +228,7 @@ class MultiAndLogDepth(Bloq):
return f"MultiAndLogDepth(n={self.n_ctrls})"
def build_call_graph(self, ssa=None):
- cost = {Toffoli(): self.n_ctrls - 1}
+ cost: Dict[Bloq, int] = {Toffoli(): self.n_ctrls - 1}
n_neg = sum(int(cv == 0) for cv in self.concrete_cvs)
if n_neg:
cost[XGate()] = 2 * n_neg
diff --git a/qualtran/bloqs/mcmt/approx_multi_toffoli_test.py b/qualtran/bloqs/mcmt/approx_multi_toffoli_test.py
index 690db1b0..91bf15be 100644
--- a/qualtran/bloqs/mcmt/approx_multi_toffoli_test.py
+++ b/qualtran/bloqs/mcmt/approx_multi_toffoli_test.py
@@ -36,6 +36,6 @@ def test_approx_multi_toffoli_classical_randomized_notebook_example():
sample_strings = tuple(random_sample_string(m) for _ in range(k))
bloq = ApproxMultiToffoli(n=n, k=k, sample_strings=sample_strings)
- _, _, target = bloq.call_classically(ctrl=int_to_bits(x_int, m))
+ _, _, target = bloq.call_classically(ctrl=int_to_bits(x_int, m)) # type: ignore
assert target == int(x_int == all_ones)
|
|
We use a linter to statically detect other issues prior to runtime. You can run it with |
|
Finally, we use an automated code formatter. When the other checks are good to go, run |
No description provided.