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

one hot encoding #918

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open

Conversation

skushnir123
Copy link
Contributor

@skushnir123 skushnir123 commented May 2, 2024

Simple bloq to implement a one-hot encoding. This will be used in a subroutine of QROM Inverse (PR forthcoming)

@fdmalone fdmalone self-requested a review May 6, 2024 15:29
Copy link
Collaborator

@fdmalone fdmalone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small nits and suggestions. Looking good!

op_tree.append(cirq.X(b[0]))
for i in range(len(a)):
for j in range(2**i):
op_tree.append(TwoBitCSwap().on_registers(ctrl=a[i], x=b[j], y=b[2**i + j]))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use cirq.CSWAP here and the it'll automatically ge tconverted to TwoBitCSwap whenever the bloq-api is used due to the _cirq_gate_to_bloq logic.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, let's change the decomposition and build_call_graph to delegate to SwapWithZero bloq



@attrs.frozen
class OneHotEncoding(GateWithRegisters):
Copy link
Collaborator

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?

Copy link
Collaborator

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 with a as control and b as target.
The part after the first X gate is SwapWithZero bloq.

image

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 set target[x][y][z] = 1 when a_x = x, a_y = y and a_z = z; you can do this again by setting target[0][0][0] = 1 and calling a 3D swap-with-zero.

Copy link
Collaborator

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.

Copy link
Collaborator

@tanujkhattar tanujkhattar May 17, 2024

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

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool

Copy link
Collaborator

@tanujkhattar tanujkhattar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll mark this as request changes following my comments above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants