-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add on each meta bloq. * Format. * Address review comments. --------- Co-authored-by: Matthew Harrigan <[email protected]>
- Loading branch information
1 parent
19c150b
commit 9c2bb7b
Showing
6 changed files
with
101 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
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,57 @@ | ||
# Copyright 2023 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. | ||
|
||
"""Classes to apply single qubit bloq to multiple qubits.""" | ||
from functools import cached_property | ||
from typing import Dict | ||
|
||
import attrs | ||
|
||
from qualtran import Bloq, BloqBuilder, Register, Signature, SoquetT | ||
|
||
|
||
@attrs.frozen | ||
class OnEach(Bloq): | ||
"""Add a single-qubit (unparameterized) bloq on each of n qubits. | ||
Args: | ||
n: the number of qubits to add the bloq to. | ||
gate: A single qubit gate. The single qubit register must be named q. | ||
Registers: | ||
- q: an n-qubit register. | ||
""" | ||
|
||
n: int | ||
gate: Bloq | ||
|
||
def __attrs_post_init__(self): | ||
assert len(self.gate.signature) == 1, "Gate must only have a single register." | ||
assert self.gate.signature[0].bitsize == 1, "Must be single qubit gate." | ||
assert self.gate.signature[0].name == 'q', "Register must be named q." | ||
|
||
@cached_property | ||
def signature(self) -> Signature: | ||
reg = Register('q', bitsize=self.n) | ||
return Signature([reg]) | ||
|
||
def short_name(self) -> str: | ||
return rf'{self.gate.short_name()}⨂{self.n}' | ||
|
||
def build_composite_bloq(self, bb: BloqBuilder, *, q: SoquetT) -> Dict[str, SoquetT]: | ||
|
||
qs = bb.split(q) | ||
for i in range(self.n): | ||
qs[i] = bb.add(self.gate, q=qs[i]) | ||
return {'q': bb.join(qs)} |
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,32 @@ | ||
# Copyright 2023 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 reduce | ||
|
||
import numpy as np | ||
|
||
import qualtran.testing as qlt_testing | ||
from qualtran.bloqs.basic_gates import Hadamard, XGate | ||
from qualtran.bloqs.on_each import OnEach | ||
|
||
|
||
def test_valid_bloq(): | ||
on_each = OnEach(10, XGate()) | ||
qlt_testing.assert_valid_bloq_decomposition(on_each) | ||
|
||
|
||
def test_tensor_contract(): | ||
bloq = OnEach(5, Hadamard()) | ||
tensor = bloq.tensor_contract() | ||
single_had = Hadamard().tensor_contract() | ||
np.testing.assert_allclose(tensor, reduce(np.kron, (single_had,) * 5)) |