-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `Negate` block encoding * Change to `GlobalPhase` * Simplify docstring
- Loading branch information
1 parent
2bd4f05
commit f8cebdb
Showing
7 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,122 @@ | ||
# 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. | ||
|
||
from functools import cached_property | ||
from typing import Dict, Set, Tuple | ||
|
||
from attrs import frozen | ||
|
||
from qualtran import bloq_example, BloqBuilder, BloqDocSpec, QAny, Register, Signature, SoquetT | ||
from qualtran.bloqs.basic_gates import GlobalPhase | ||
from qualtran.bloqs.block_encoding import BlockEncoding | ||
from qualtran.bloqs.block_encoding.lcu_select_and_prepare import PrepareOracle | ||
from qualtran.resource_counting import BloqCountT, SympySymbolAllocator | ||
from qualtran.symbolics import SymbolicFloat, SymbolicInt | ||
|
||
|
||
@frozen | ||
class Phase(BlockEncoding): | ||
r"""Apply a phase to a block encoding. | ||
Given $B[A]$ as a $(\alpha, a, \epsilon)$-block encoding of $A$, produces a | ||
$(\alpha, a, \epsilon)$-block encoding of $\exp(i\pi\phi)A$. | ||
Args: | ||
block_encoding: The block encoding to apply a phase to. | ||
phi: The phase angle. | ||
eps: The precision of the phase angle. | ||
Registers: | ||
system: The system register. | ||
ancilla: The ancilla register (present only if bitsize > 0). | ||
resource: The resource register (present only if bitsize > 0). | ||
""" | ||
|
||
block_encoding: BlockEncoding | ||
phi: SymbolicFloat | ||
eps: SymbolicFloat | ||
|
||
@property | ||
def alpha(self) -> SymbolicFloat: | ||
return self.block_encoding.alpha | ||
|
||
@property | ||
def system_bitsize(self) -> SymbolicInt: | ||
return self.block_encoding.system_bitsize | ||
|
||
@property | ||
def ancilla_bitsize(self) -> SymbolicInt: | ||
return self.block_encoding.ancilla_bitsize | ||
|
||
@property | ||
def resource_bitsize(self) -> SymbolicInt: | ||
return self.block_encoding.resource_bitsize | ||
|
||
@property | ||
def epsilon(self) -> SymbolicFloat: | ||
return self.block_encoding.epsilon + self.eps | ||
|
||
@cached_property | ||
def signature(self) -> Signature: | ||
return Signature.build_from_dtypes( | ||
system=QAny(self.system_bitsize), | ||
ancilla=QAny(self.ancilla_bitsize), # if ancilla_bitsize is 0, not present | ||
resource=QAny(self.resource_bitsize), # if ancilla_bitsize is 0, not present | ||
) | ||
|
||
def pretty_name(self) -> str: | ||
return f"B[exp({self.phi}i){self.block_encoding.pretty_name()[2:-1]}]" | ||
|
||
@property | ||
def target_registers(self) -> Tuple[Register, ...]: | ||
return tuple(self.signature.rights()) | ||
|
||
@property | ||
def junk_registers(self) -> Tuple[Register, ...]: | ||
return (self.signature.get_right("resource"),) if self.resource_bitsize > 0 else () | ||
|
||
@property | ||
def selection_registers(self) -> Tuple[Register, ...]: | ||
return (self.signature.get_right("ancilla"),) if self.ancilla_bitsize > 0 else () | ||
|
||
@property | ||
def signal_state(self) -> PrepareOracle: | ||
# This method will be implemented in the future after PrepareOracle | ||
# is updated for the BlockEncoding interface. | ||
# GitHub issue: https://github.com/quantumlib/Qualtran/issues/1104 | ||
raise NotImplementedError | ||
|
||
def build_call_graph(self, ssa: SympySymbolAllocator) -> Set[BloqCountT]: | ||
return {(self.block_encoding, 1), (GlobalPhase(exponent=self.phi, eps=self.eps), 1)} | ||
|
||
def build_composite_bloq(self, bb: BloqBuilder, **soqs: SoquetT) -> Dict[str, SoquetT]: | ||
bb.add(GlobalPhase(exponent=self.phi, eps=self.eps)) | ||
|
||
return bb.add_d(self.block_encoding, **soqs) | ||
|
||
|
||
@bloq_example | ||
def _phase_block_encoding() -> Phase: | ||
from qualtran.bloqs.basic_gates import Hadamard | ||
from qualtran.bloqs.block_encoding.unitary import Unitary | ||
|
||
phase_block_encoding = Phase(Unitary(Hadamard()), phi=0.25, eps=0) | ||
return phase_block_encoding | ||
|
||
|
||
_PHASE_DOC = BloqDocSpec( | ||
bloq_cls=Phase, | ||
import_line="from qualtran.bloqs.block_encoding import Phase", | ||
examples=[_phase_block_encoding], | ||
) |
This file contains 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,50 @@ | ||
# 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 | ||
|
||
from qualtran import QAny, Register, Signature | ||
from qualtran.bloqs.basic_gates import Hadamard | ||
from qualtran.bloqs.block_encoding.phase import _phase_block_encoding, Phase | ||
from qualtran.bloqs.block_encoding.unitary import Unitary | ||
|
||
|
||
def test_phase(bloq_autotester): | ||
bloq_autotester(_phase_block_encoding) | ||
|
||
|
||
def test_phase_signature(): | ||
assert _phase_block_encoding().signature == Signature([Register("system", QAny(1))]) | ||
|
||
|
||
def test_phase_params(): | ||
bloq = _phase_block_encoding() | ||
assert bloq.alpha == 1 | ||
assert bloq.epsilon == 0 | ||
assert bloq.ancilla_bitsize == 0 | ||
assert bloq.resource_bitsize == 0 | ||
|
||
|
||
def test_phase_tensors(): | ||
bloq = _phase_block_encoding() | ||
from_gate = np.exp(0.25j * np.pi) * Hadamard().tensor_contract() | ||
from_tensors = bloq.tensor_contract() | ||
np.testing.assert_allclose(from_gate, from_tensors) | ||
|
||
|
||
def test_negate(): | ||
bloq = Phase(Unitary(Hadamard()), phi=1, eps=0) | ||
from_gate = -Hadamard().tensor_contract() | ||
from_tensors = bloq.tensor_contract() | ||
np.testing.assert_allclose(from_gate, from_tensors) |
This file contains 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 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