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

Implemented State Preparation Gate #4482

Merged
merged 20 commits into from Sep 16, 2021

Conversation

AnimeshSinha1309
Copy link
Contributor

@AnimeshSinha1309 AnimeshSinha1309 commented Sep 3, 2021

Implements a new gate to reset on all channels and prepare an arbitrary state over n-qubits by returning the Kraus operator for the same. Associated tests included.

Closes #4119.

Just the basic class setup.
No tests yet, just trying to import and run, basic framework ready, think this is how to make the state prep gate, by decomposing to cirq.reset() and cirq.MatrixGate().
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Sep 3, 2021
@AnimeshSinha1309 AnimeshSinha1309 marked this pull request as draft September 3, 2021 09:57
@CirqBot CirqBot added the size: M 50< lines changed <250 label Sep 3, 2021
Now the prepare gate gets imported in my local environment.
The code is almost complete, it should be very close to working, but the simulator refuses to simulate.
State preparation gate has all it's tests working, but it's still wrong, because changing the values of the target state in test_state_prep_gate leads to the unitary not being a unitary anymore.
A one line formatting error fixed in state preparation gate.
Tested on all real values, everything works. Fails on complex valued states.
@AnimeshSinha1309 AnimeshSinha1309 marked this pull request as ready for review September 4, 2021 19:09
Needed to take the conjugate in Gram-Schmidt process, now works.
Trying to get all complex cases to work. Ran autoformatter.
Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

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

My overall impression of this PR is positive - the new gate is fully realized as a Cirq type, with protocol methods, documentation, and testing in line with existing code. Comments are mostly style fixes and requests for additional tests.

Thanks for taking this on!

cirq-core/cirq/ops/state_preparation_gate.py Outdated Show resolved Hide resolved
cirq-core/cirq/ops/state_preparation_gate.py Outdated Show resolved Hide resolved
cirq-core/cirq/ops/state_preparation_gate.py Outdated Show resolved Hide resolved
cirq-core/cirq/ops/state_preparation_gate.py Outdated Show resolved Hide resolved
cirq-core/cirq/ops/state_preparation_gate.py Outdated Show resolved Hide resolved
cirq-core/cirq/ops/state_preparation_gate_test.py Outdated Show resolved Hide resolved
cirq-core/cirq/ops/state_preparation_gate_test.py Outdated Show resolved Hide resolved
cirq-core/cirq/ops/state_preparation_gate.py Outdated Show resolved Hide resolved
cirq-core/cirq/ops/state_preparation_gate_test.py Outdated Show resolved Hide resolved
cirq-core/cirq/ops/state_preparation_gate.py Outdated Show resolved Hide resolved
Changed the tests to be parametrized, moved a few functions around, fixed some comments.
A different implementation, doesn't decompose to a reset and a unitary, instead uses a kraus operator to set the values of the qubits. Adds a new test and match parameters in the pytest fail checks.
Equality for non-compatible datatypes, few additions in tests to get full coverage.
Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

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

Commented on the remaining items.

@fmozafari
Copy link

fmozafari commented Sep 16, 2021

@95-martin-orion : Hi, I have a question. I have studied several algorithms for preparing quantum states. There are different parameters. For example, some of them reduce circuit cost and depth but using ancilla qubits. Some of them are efficient for sparse states and etc. Hence, we can have different decompositions for different goals. Now my question is, where can I implement them? as a "decompose" function in the "state preparation gate" class? Thanks!

Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

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

Looks good!

@95-martin-orion
Copy link
Collaborator

@95-martin-orion : Hi, I have a question. I have studied several algorithms for preparing quantum states {...} where can I implement them? as a "decompose" function in the "state preparation gate" class? Thanks!

We only support a single _decompose_ method per class - I think an optimizer may be more appropriate for this. For example, if you have a device with a convenient state-preparation method, you could optimize StatePreparationChannel into that method as part of the device-specific optimizer.

@95-martin-orion 95-martin-orion added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Sep 16, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Sep 16, 2021
@CirqBot CirqBot merged commit 3725c6a into quantumlib:master Sep 16, 2021
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Sep 16, 2021
def __repr__(self) -> str:
return f'cirq.StatePreparationChannel({proper_repr(self._state)})'

def __eq__(self, other) -> bool:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not use @value.value_equality ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Having problems with this, mentioned in the new PR.

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.

@AnimeshSinha1309 Left a few nit's. Please fix in a follow up PR.

Thanks for doing this!



def test_equality_of_gates():
state = np.array([1, 0, 0, 0], dtype=np.complex64)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use EqualsTester instead to check for equality. It will also help check that two StatePreparationChannels with different states are not equal.

@tanujkhattar
Copy link
Collaborator

@fmozafari I've opened #4500 to track this.

AnimeshSinha1309 added a commit to AnimeshSinha1309/contrib-cirq that referenced this pull request Sep 17, 2021
Fixed many of the nits mentioned in quantumlib#4482, value-equality not a part of this commit.
tanujkhattar pushed a commit that referenced this pull request Sep 28, 2021
* Fixing Minor Errors

Fixed many of the nits mentioned in #4482, value-equality not a part of this commit.

* Approx Equality support added

State Preparation Channel got equality comparison similar to that of Matrix Gate.

* Added name parameter to state preparation channel

name parameter added to constructor, JSON, and repr serialization as well as test data.

* Custom names in serialization tests

The name has been changed in the JSON and repr protocol tests to make the older version fail.

* Format and Lint Fix

Had to run pylint.

* Adding tests to ignore name

Two gates with different names are still equal, added tests for that.
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
Implements a new gate to reset on all channels and prepare an arbitrary state over n-qubits by returning the Kraus operator for the same. Associated tests included.

Closes quantumlib#4119.
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
* Fixing Minor Errors

Fixed many of the nits mentioned in quantumlib#4482, value-equality not a part of this commit.

* Approx Equality support added

State Preparation Channel got equality comparison similar to that of Matrix Gate.

* Added name parameter to state preparation channel

name parameter added to constructor, JSON, and repr serialization as well as test data.

* Custom names in serialization tests

The name has been changed in the JSON and repr protocol tests to make the older version fail.

* Format and Lint Fix

Had to run pylint.

* Adding tests to ignore name

Two gates with different names are still equal, added tests for that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining. size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: state preparation gate
6 participants