-
Notifications
You must be signed in to change notification settings - Fork 50
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
one hot encoding #918
Open
skushnir123
wants to merge
26
commits into
quantumlib:main
Choose a base branch
from
skushnir123:one-hot-encoding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
one hot encoding #918
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
8b79536
one hot encoding
skushnir123 c1f3fb1
Update one_hot_encoding.py
skushnir123 4141ba1
nit change
skushnir123 63ca012
switched endianness
skushnir123 79d75c9
Update one_hot_encoding.py
skushnir123 2258be7
Update one_hot_encoding.py
skushnir123 e294fd3
Update one_hot_encoding.py
skushnir123 48b7e59
Update one_hot_encoding.py
skushnir123 626a4f7
added one-sided register
skushnir123 5c9447d
changing to RIGHT registers
skushnir123 9d81e17
Update one_hot_encoding.py
skushnir123 b176982
Update one_hot_encoding.py
skushnir123 9ea4a4e
reformat
skushnir123 16ff44f
Update one_hot_encoding.py
skushnir123 55cc80b
Update one_hot_encoding_test.py
skushnir123 6ba5ec0
Update one_hot_encoding_test.py
skushnir123 c192e22
Update one_hot_encoding.py
skushnir123 0e6df6b
Update one_hot_encoding_test.py
skushnir123 887950f
Update one_hot_encoding_test.py
skushnir123 fc5c373
Merge branch 'main' into one-hot-encoding
fdmalone 5f5a385
Merge branch 'main' into one-hot-encoding
mpharrigan 7bbfdd2
Update one_hot_encoding.py
skushnir123 8cad959
Merge branch 'one-hot-encoding' of https://github.com/skushnir123/Qua…
skushnir123 ee51728
Update one_hot_encoding.py
skushnir123 8700910
one_hot_encoding test
skushnir123 7dcb54e
Merge branch 'main' into one-hot-encoding
tanujkhattar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# 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. | ||
import attrs | ||
import cirq | ||
from attr import field | ||
from numpy._typing import NDArray | ||
|
||
from qualtran import GateWithRegisters, QAny, QUInt, Signature | ||
from qualtran.bloqs.basic_gates import TwoBitCSwap | ||
|
||
|
||
@attrs.frozen | ||
class OneHotEncoding(GateWithRegisters): | ||
""" | ||
skushnir123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
One-hot encode a binary integer into a target register. | ||
|
||
Registers: | ||
a: an unsigned integer | ||
b: the target to one-hot encode. | ||
|
||
References: | ||
[Windowed quantum arithmetic](https://arxiv.org/pdf/1905.07682.pdf) | ||
skushnir123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Figure 4] | ||
""" | ||
|
||
binary_reg_size: int = field() | ||
skushnir123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@property | ||
def signature(self) -> 'Signature': | ||
return Signature.build_from_dtypes( | ||
a=QUInt(self.binary_reg_size), b=QAny(2**self.binary_reg_size) | ||
) | ||
|
||
def decompose_from_registers( | ||
self, *, context: cirq.DecompositionContext, **quregs: NDArray[cirq.Qid] | ||
) -> cirq.OP_TREE: | ||
a = quregs['a'] | ||
b = quregs['b'] | ||
|
||
yield cirq.X(b[0]) | ||
for i in range(len(a)): | ||
for j in range(2**i): | ||
yield TwoBitCSwap().on_registers(ctrl=a[i], x=b[j], y=b[2**i + j]) |
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. | ||
import attrs | ||
import cirq | ||
import pytest | ||
from attr import field | ||
from numpy._typing import NDArray | ||
|
||
from qualtran import GateWithRegisters, QUInt, Signature | ||
from qualtran.bloqs.data_loading.one_hot_encoding import OneHotEncoding | ||
from qualtran.cirq_interop.bit_tools import iter_bits | ||
from qualtran.cirq_interop.testing import assert_circuit_inp_out_cirqsim | ||
|
||
|
||
@attrs.frozen | ||
class OneHotEncodingTest(GateWithRegisters): | ||
integer: int = field() | ||
size: int = field() | ||
|
||
@property | ||
def signature(self) -> 'Signature': | ||
return Signature.build_from_dtypes(a=QUInt(self.size), b=QUInt(2**self.size)) | ||
|
||
skushnir123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def decompose_from_registers( | ||
self, *, context: cirq.DecompositionContext, **quregs: NDArray[cirq.Qid] | ||
) -> cirq.OP_TREE: | ||
a = quregs['a'] | ||
skushnir123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
b = quregs['b'] | ||
binary_repr = list(iter_bits(self.integer, self.size))[::-1] | ||
for i in range(self.size): | ||
if binary_repr[i] == 1: | ||
yield cirq.X(a[i]) | ||
yield OneHotEncoding(binary_reg_size=self.size).on_registers(a=a, b=b) | ||
|
||
|
||
@pytest.mark.parametrize('integer', list(range(8))) | ||
fdmalone marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_one_hot_encoding(integer): | ||
gate = OneHotEncodingTest(integer, 3) | ||
qubits = cirq.LineQubit.range(3 + 2**3) | ||
op = gate.on_registers(a=qubits[:3], b=qubits[3:]) | ||
circuit0 = cirq.Circuit(op) | ||
initial_state = [0] * (3 + 2**3) | ||
final_state = [0] * (3 + 2**3) | ||
final_state[:3] = list(iter_bits(integer, 3))[::-1] | ||
final_state[3 + integer] = 1 | ||
assert_circuit_inp_out_cirqsim(circuit0, qubits, initial_state, final_state) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this same as the
SwapWithZero
gate we already have?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can essentially write a one hot encoding bloq by doing an X(q[0]) and then call
SwapWithZero
witha
as control andb
as target.The part after the first
X
gate isSwapWithZero
bloq.Also,
SwapWithZero
now supports N-dimensional registers so you can also do a one-hot for a multi dimensional input / output register. For example, if you have a 3d data where the input is 3 selection registers (a_x
,a_y
,a_z
) and target is a 3D encoding where you would settarget[x][y][z] = 1
whena_x = x
,a_y = y
anda_z = z
; you can do this again by settingtarget[0][0][0] = 1
and calling a 3D swap-with-zero.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no issue with swap with zero destroying the rest of the "b" register here? I've always been confused by when SwapWithZero is ok to use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The |B> register is assumed to be |0> in this case, so it doesn't matter how you permute it it will continue to be 0 everywhere except the x'th bit where you'll have a 1 since you swapped 0 <-> x .
The circuit after the first X gate is the same circuit as used for SwapWithZero
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool