Skip to content

Commit

Permalink
fix QPE test failure, add non-slow bloq example autotest (#1487)
Browse files Browse the repository at this point in the history
* fix QPE test failure, add non-slow bloq example autotest

* fix bug: `cirq.ControlledGate` fails when it can't detect that bloq is unitary

* regen notebook

* add comment on controlled unitary failure
  • Loading branch information
anurudhp authored Nov 7, 2024
1 parent 7497989 commit 188b663
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
16 changes: 13 additions & 3 deletions qualtran/_infra/controlled.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,19 @@ def _unitary_(self):
# subbloq is a cirq gate, use the cirq-style API to derive a unitary.
import cirq

return cirq.unitary(
cirq.ControlledGate(self.subbloq, control_values=self.ctrl_spec.to_cirq_cv())
)
# TODO It would be ideal to use `tensor_contract` always,
# but at the moment it's about 5-10x slower than `cirq.unitary`.
# So we default to `cirq.unitary`, and only use `tensor_contract` if it fails.
# https://github.com/quantumlib/Qualtran/issues/1336
# TODO `cirq.ControlledGate` fails to correctly verify `subbloq` using
# a compute-uncompute `And` pair is unitary.
# https://github.com/quantumlib/Qualtran/issues/1488
try:
return cirq.unitary(
cirq.ControlledGate(self.subbloq, control_values=self.ctrl_spec.to_cirq_cv())
)
except ValueError:
pass # use the tensor contraction instead
if all(reg.side == Side.THRU for reg in self.subbloq.signature):
# subbloq has only THRU registers, so the tensor contraction corresponds
# to a unitary matrix.
Expand Down
21 changes: 18 additions & 3 deletions qualtran/bloqs/phase_estimation/qubitization_qpe.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"\n",
"#### Parameters\n",
" - `walk`: Bloq representing the Qubitization walk operator to run the phase estimation protocol on.\n",
" - `m_bits`: Bitsize of the phase register to be used during phase estimation.\n",
" - `ctrl_state_prep`: Bloq to prepare the control state on the phase register. Defaults to `OnEach(self.m_bits, Hadamard())`.\n",
" - `qft_inv`: Bloq to apply inverse QFT on the phase register. Defaults to `QFTTextBook(self.m_bits).adjoint()` \n",
"\n",
Expand Down Expand Up @@ -193,6 +192,22 @@
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "49743657",
"metadata": {
"cq.autogen": "QubitizationQPE.qubitization_qpe_ising"
},
"outputs": [],
"source": [
"from qualtran.bloqs.chemistry.ising.walk_operator import get_walk_operator_for_1d_ising_model\n",
"from qualtran.bloqs.phase_estimation import RectangularWindowState\n",
"\n",
"walk, _ = get_walk_operator_for_1d_ising_model(4, 0.1)\n",
"qubitization_qpe_ising = QubitizationQPE(walk, RectangularWindowState(4))"
]
},
{
"cell_type": "markdown",
"id": "b19f9365",
Expand All @@ -213,8 +228,8 @@
"outputs": [],
"source": [
"from qualtran.drawing import show_bloqs\n",
"show_bloqs([qubitization_qpe_hubbard_model_small, qubitization_qpe_sparse_chem, qubitization_qpe_chem_thc],\n",
" ['`qubitization_qpe_hubbard_model_small`', '`qubitization_qpe_sparse_chem`', '`qubitization_qpe_chem_thc`'])"
"show_bloqs([qubitization_qpe_hubbard_model_small, qubitization_qpe_sparse_chem, qubitization_qpe_chem_thc, qubitization_qpe_ising],\n",
" ['`qubitization_qpe_hubbard_model_small`', '`qubitization_qpe_sparse_chem`', '`qubitization_qpe_chem_thc`', '`qubitization_qpe_ising`'])"
]
},
{
Expand Down
14 changes: 12 additions & 2 deletions qualtran/bloqs/phase_estimation/qubitization_qpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class QubitizationQPE(GateWithRegisters):
Args:
walk: Bloq representing the Qubitization walk operator to run the phase estimation protocol
on.
m_bits: Bitsize of the phase register to be used during phase estimation.
ctrl_state_prep: Bloq to prepare the control state on the phase register. Defaults to
`OnEach(self.m_bits, Hadamard())`.
qft_inv: Bloq to apply inverse QFT on the phase register. Defaults to
Expand Down Expand Up @@ -119,7 +118,7 @@ def decompose_from_registers(
qpre_reg = quregs['qpe_reg']

yield self.ctrl_state_prep.on(*qpre_reg)
yield walk_controlled.on_registers(**walk_regs, control=qpre_reg[-1])
yield walk_controlled.on_registers(**walk_regs, ctrl=qpre_reg[-1])
walk = self.walk**2
for i in range(self.m_bits - 2, -1, -1):
yield reflect_controlled.on_registers(control=qpre_reg[i], **reflect_regs)
Expand All @@ -143,6 +142,16 @@ def __str__(self) -> str:
return f'QubitizationQPE[{self.m_bits}]'


@bloq_example
def _qubitization_qpe_ising() -> QubitizationQPE:
from qualtran.bloqs.chemistry.ising.walk_operator import get_walk_operator_for_1d_ising_model
from qualtran.bloqs.phase_estimation import RectangularWindowState

walk, _ = get_walk_operator_for_1d_ising_model(4, 0.1)
qubitization_qpe_ising = QubitizationQPE(walk, RectangularWindowState(4))
return qubitization_qpe_ising


@bloq_example
def _qubitization_qpe_hubbard_model_small() -> QubitizationQPE:
import numpy as np
Expand Down Expand Up @@ -252,5 +261,6 @@ def _qubitization_qpe_sparse_chem() -> QubitizationQPE:
_qubitization_qpe_hubbard_model_small,
_qubitization_qpe_sparse_chem,
_qubitization_qpe_chem_thc,
_qubitization_qpe_ising,
),
)
10 changes: 10 additions & 0 deletions qualtran/bloqs/phase_estimation/qubitization_qpe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from qualtran.bloqs.phase_estimation.qubitization_qpe import (
_qubitization_qpe_chem_thc,
_qubitization_qpe_hubbard_model_small,
_qubitization_qpe_ising,
_qubitization_qpe_sparse_chem,
QubitizationQPE,
)
Expand All @@ -29,6 +30,10 @@
from qualtran.testing import execute_notebook


def test_ising_example(bloq_autotester):
bloq_autotester(_qubitization_qpe_ising)


@pytest.mark.slow
def test_qubitization_qpe_bloq_autotester(bloq_autotester):
bloq_autotester(_qubitization_qpe_hubbard_model_small)
Expand Down Expand Up @@ -103,3 +108,8 @@ def test_qubitization_phase_estimation_of_walk(num_terms: int, use_resource_stat
@pytest.mark.notebook
def test_phase_estimation_of_qubitized_hubbard_model():
execute_notebook('phase_estimation_of_quantum_walk')


@pytest.mark.notebook
def test_notebook():
execute_notebook('qubitization_qpe')
1 change: 1 addition & 0 deletions qualtran/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def assert_bloq_example_serializes_for_pytest(bloq_ex: BloqExample):
'qubitization_qpe_chem_thc', # too slow
'walk_op_chem_sparse',
'qubitization_qpe_sparse_chem', # too slow
'qubitization_qpe_ising',
'trott_unitary',
'symbolic_hamsim_by_gqsp',
'gf16_addition', # cannot serialize QGF
Expand Down

0 comments on commit 188b663

Please sign in to comment.