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

ControlledViaAnd tests and bloq examples #1182

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dev_tools/autogenerate-bloqs-notebooks-v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import qualtran.bloqs.factoring.mod_exp
import qualtran.bloqs.hamiltonian_simulation.hamiltonian_simulation_by_gqsp
import qualtran.bloqs.mcmt.and_bloq
import qualtran.bloqs.mcmt.controlled_via_and
import qualtran.bloqs.mcmt.ctrl_spec_and
import qualtran.bloqs.mod_arithmetic.mod_addition
import qualtran.bloqs.multiplexers.apply_gate_to_lth_target
Expand Down Expand Up @@ -224,6 +225,11 @@
module=qualtran.bloqs.mcmt.ctrl_spec_and,
bloq_specs=[qualtran.bloqs.mcmt.ctrl_spec_and._CTRLSPEC_AND_DOC],
),
NotebookSpecV2(
title='Multi control bloq via single control bloq and `And` ladder',
module=qualtran.bloqs.mcmt.controlled_via_and,
bloq_specs=[qualtran.bloqs.mcmt.controlled_via_and._CONTROLLED_VIA_AND_DOC],
),
]


Expand Down
1 change: 1 addition & 0 deletions docs/bloqs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Bloqs Library
basic_gates/identity.ipynb
bookkeeping/bookkeeping.ipynb
mcmt/ctrl_spec_and.ipynb
mcmt/controlled_via_and.ipynb

.. toctree::
:maxdepth: 2
Expand Down
1 change: 1 addition & 0 deletions qualtran/bloqs/mcmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from qualtran.bloqs.mcmt.and_bloq import And, MultiAnd
from qualtran.bloqs.mcmt.controlled_via_and import ControlledViaAnd
from qualtran.bloqs.mcmt.ctrl_spec_and import CtrlSpecAnd
from qualtran.bloqs.mcmt.multi_control_multi_target_pauli import (
MultiControlPauli,
Expand Down
170 changes: 170 additions & 0 deletions qualtran/bloqs/mcmt/controlled_via_and.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "fa539e78",
"metadata": {
"cq.autogen": "title_cell"
},
"source": [
"# Multi control bloq via single control bloq and `And` ladder"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bcb7aec6",
"metadata": {
"cq.autogen": "top_imports"
},
"outputs": [],
"source": [
"from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n",
"from qualtran import QBit, QInt, QUInt, QAny\n",
"from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n",
"from typing import *\n",
"import numpy as np\n",
"import sympy\n",
"import cirq"
]
},
{
"cell_type": "markdown",
"id": "0653bdfc",
"metadata": {
"cq.autogen": "ControlledViaAnd.bloq_doc.md"
},
"source": [
"## `ControlledViaAnd`\n",
"Reduces a generic controlled bloq to a singly-controlled bloq using an And ladder.\n",
"\n",
"Implements a generic controlled version of the subbloq, by first reducing the\n",
"arbitrary control to a single qubit, and then using a single-qubit-controlled\n",
"variant of the subbloq.\n",
"\n",
"For signature, see :class:`Controlled`.\n",
"\n",
"#### Parameters\n",
" - `subbloq`: The bloq we are controlling.\n",
" - `ctrl_spec`: The specification for how to control the bloq.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cdeee9e0",
"metadata": {
"cq.autogen": "ControlledViaAnd.bloq_doc.py"
},
"outputs": [],
"source": [
"from qualtran.bloqs.mcmt import ControlledViaAnd"
]
},
{
"cell_type": "markdown",
"id": "63037b5b",
"metadata": {
"cq.autogen": "ControlledViaAnd.example_instances.md"
},
"source": [
"### Example Instances"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "789e7f88",
"metadata": {
"cq.autogen": "ControlledViaAnd.controlled_via_and_ints"
},
"outputs": [],
"source": [
"from qualtran import CtrlSpec, QInt, QUInt\n",
"from qualtran.bloqs.basic_gates import Hadamard\n",
"\n",
"controlled_via_and_ints = ControlledViaAnd(\n",
" Hadamard(),\n",
" CtrlSpec(\n",
" qdtypes=(QUInt(4), QInt(4)), cvs=(np.array([0, 1, 2, 3]), np.array([0, 1, -1, -2]))\n",
" ),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7140523f",
"metadata": {
"cq.autogen": "ControlledViaAnd.controlled_via_and_qbits"
},
"outputs": [],
"source": [
"from qualtran.bloqs.basic_gates import Hadamard\n",
"\n",
"controlled_via_and_qbits = ControlledViaAnd(Hadamard(), CtrlSpec(cvs=(np.array([0, 1, 1, 0]),)))"
]
},
{
"cell_type": "markdown",
"id": "27d4d6e1",
"metadata": {
"cq.autogen": "ControlledViaAnd.graphical_signature.md"
},
"source": [
"#### Graphical Signature"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "45a0473b",
"metadata": {
"cq.autogen": "ControlledViaAnd.graphical_signature.py"
},
"outputs": [],
"source": [
"from qualtran.drawing import show_bloqs\n",
"show_bloqs([controlled_via_and_ints, controlled_via_and_qbits],\n",
" ['`controlled_via_and_ints`', '`controlled_via_and_qbits`'])"
]
},
{
"cell_type": "markdown",
"id": "b0301e73",
"metadata": {
"cq.autogen": "ControlledViaAnd.call_graph.md"
},
"source": [
"### Call Graph"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9af47394",
"metadata": {
"cq.autogen": "ControlledViaAnd.call_graph.py"
},
"outputs": [],
"source": [
"from qualtran.resource_counting.generalizers import ignore_split_join\n",
"controlled_via_and_ints_g, controlled_via_and_ints_sigma = controlled_via_and_ints.call_graph(max_depth=1, generalizer=ignore_split_join)\n",
"show_call_graph(controlled_via_and_ints_g)\n",
"show_counts_sigma(controlled_via_and_ints_sigma)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
29 changes: 28 additions & 1 deletion qualtran/bloqs/mcmt/controlled_via_and.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import numpy as np
from attrs import frozen

from qualtran import Bloq, Controlled, CtrlSpec
from qualtran import Bloq, bloq_example, BloqDocSpec, Controlled, CtrlSpec
from qualtran.bloqs.basic_gates import XGate
from qualtran.bloqs.mcmt.ctrl_spec_and import CtrlSpecAnd

Expand Down Expand Up @@ -125,3 +125,30 @@ def build_call_graph(self, ssa: 'SympySymbolAllocator') -> set['BloqCountT']:
counts[ctrl.adjoint()] += 1

return set(counts.items())


@bloq_example
def _controlled_via_and_qbits() -> ControlledViaAnd:
from qualtran.bloqs.basic_gates import Hadamard

controlled_via_and_qbits = ControlledViaAnd(Hadamard(), CtrlSpec(cvs=(np.array([0, 1, 1, 0]),)))
return controlled_via_and_qbits


@bloq_example
def _controlled_via_and_ints() -> ControlledViaAnd:
from qualtran import CtrlSpec, QInt, QUInt
from qualtran.bloqs.basic_gates import Hadamard

controlled_via_and_ints = ControlledViaAnd(
Hadamard(),
CtrlSpec(
qdtypes=(QUInt(4), QInt(4)), cvs=(np.array([0, 1, 2, 3]), np.array([0, 1, -1, -2]))
),
)
return controlled_via_and_ints


_CONTROLLED_VIA_AND_DOC = BloqDocSpec(
bloq_cls=ControlledViaAnd, examples=[_controlled_via_and_ints, _controlled_via_and_qbits]
)
49 changes: 49 additions & 0 deletions qualtran/bloqs/mcmt/controlled_via_and_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2024 Google LLC
#
# 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
#
# https://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 numpy as np
import pytest

from qualtran import Controlled, CtrlSpec, QInt, QUInt
from qualtran.bloqs.for_testing.matrix_gate import MatrixGate
from qualtran.bloqs.mcmt.controlled_via_and import (
_controlled_via_and_ints,
_controlled_via_and_qbits,
ControlledViaAnd,
)


def test_examples(bloq_autotester):
bloq_autotester(_controlled_via_and_qbits)
bloq_autotester(_controlled_via_and_ints)


@pytest.mark.parametrize(
"ctrl_spec",
[
CtrlSpec(QInt(4), [1, -2]),
CtrlSpec(QUInt(4), [2, 5]),
CtrlSpec((QInt(2), QUInt(2)), cvs=([1, -2], [2, 3])),
],
)
def test_tensor_against_naive_controlled(ctrl_spec: CtrlSpec):
rs = np.random.RandomState(42)
subbloq = MatrixGate.random(2, random_state=rs)

cbloq = ControlledViaAnd(subbloq, ctrl_spec)
naive_cbloq = Controlled(subbloq, ctrl_spec)

expected_tensor = naive_cbloq.tensor_contract()
actual_tensor = cbloq.tensor_contract()

np.testing.assert_allclose(expected_tensor, actual_tensor)
Loading