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

Diagonal gate #3890

Merged
merged 21 commits into from
Mar 12, 2021
Merged

Diagonal gate #3890

merged 21 commits into from
Mar 12, 2021

Conversation

bichengying
Copy link
Collaborator

Add an N-qubit DiagonalGate, which is specified in #3866. The decomposition algorithm (2^n-1 Rz gates and 2^n -2 CNOT gates) is implemented based on the paper:

Welch, Jonathan, et al. "Efficient quantum circuits for diagonal unitaries without ancillas." New Journal of Physics 16.3 (2014): 033040. https://iopscience.iop.org/article/10.1088/1367-2630/16/3/033040/meta

For the step-by-step explanation of the implemention, please refer this colab.

A current known issue is that the decomposition algorithm has a global phase shift between the diagonal unitary matrix and the decomposed circuit implementation. It can produce a surprising effect if the diagonal gates is used within a partial system, such as under the controlled gate. Hence, a GlobalPhaseOperation is added into the _decompose_ method. But GlobalPhaseOperation doesn't have as universal support as the single-/two-qubit gates. So adding GlobalPhaseOperation may limit the usage of DiagonalGate.

@bichengying bichengying requested review from cduck, vtomole and a team as code owners March 9, 2021 00:38
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Mar 9, 2021
@vtomole
Copy link
Collaborator

vtomole commented Mar 9, 2021

Call the file and classes diagonal_gate instead of n_qubit_diagonal_gate.

Copy link
Contributor

@balopat balopat left a comment

Choose a reason for hiding this comment

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

Looking great, I found a bunch of nits though. +1 to renaming the file to diagonal_gate.py.

cirq/ops/n_qubit_diagonal_gate_test.py Outdated Show resolved Hide resolved
cirq/ops/n_qubit_diagonal_gate.py Outdated Show resolved Hide resolved
cirq/ops/n_qubit_diagonal_gate.py Outdated Show resolved Hide resolved
cirq/ops/n_qubit_diagonal_gate.py Outdated Show resolved Hide resolved
cirq/ops/n_qubit_diagonal_gate.py Outdated Show resolved Hide resolved
cirq/ops/n_qubit_diagonal_gate.py Outdated Show resolved Hide resolved
cirq/ops/n_qubit_diagonal_gate.py Outdated Show resolved Hide resolved
cirq/ops/n_qubit_diagonal_gate.py Outdated Show resolved Hide resolved
cirq/ops/n_qubit_diagonal_gate.py Outdated Show resolved Hide resolved
cirq/ops/n_qubit_diagonal_gate.py Outdated Show resolved Hide resolved
bichengying and others added 10 commits March 10, 2021 17:54
Co-authored-by: Balint Pato <balopat@users.noreply.github.com>
Co-authored-by: Balint Pato <balopat@users.noreply.github.com>
Co-authored-by: Balint Pato <balopat@users.noreply.github.com>
Co-authored-by: Balint Pato <balopat@users.noreply.github.com>
Co-authored-by: Balint Pato <balopat@users.noreply.github.com>
Co-authored-by: Balint Pato <balopat@users.noreply.github.com>
Co-authored-by: Balint Pato <balopat@users.noreply.github.com>
Co-authored-by: Balint Pato <balopat@users.noreply.github.com>
@bichengying
Copy link
Collaborator Author

Thanks for reviewing! The file has been renamed to diagonal_gate. About parameterized DiagonalGate comment I have another question. Please take a look again.

# We do not support the decomposition of parameterized case yet.
# So cirq.decompose should do nothing.
assert len(cirq.decompose(op)) == 1
assert cirq.decompose(op)[0] == op

Copy link
Contributor

Choose a reason for hiding this comment

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

you can simplify these two lines to assert cirq.decompose(op) == [op]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

@balopat balopat left a comment

Choose a reason for hiding this comment

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

LGTM, great! One more tiny nit

bichengying and others added 2 commits March 12, 2021 10:34
Co-authored-by: Balint Pato <balopat@users.noreply.github.com>
@balopat
Copy link
Contributor

balopat commented Mar 12, 2021

One more thing: cirq.contrib.acquaintance.executor_test.py has a test DiagonalGate defined. Can you replace that with the new shiny DiagonalGate? Thanks!

1 similar comment
@balopat
Copy link
Contributor

balopat commented Mar 12, 2021

One more thing: cirq.contrib.acquaintance.executor_test.py has a test DiagonalGate defined. Can you replace that with the new shiny DiagonalGate? Thanks!

@bichengying
Copy link
Collaborator Author

One more thing: cirq.contrib.acquaintance.executor_test.py has a test DiagonalGate defined. Can you replace that with the new shiny DiagonalGate? Thanks!

Sure. Done.

@balopat balopat added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Mar 12, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Mar 12, 2021
@CirqBot CirqBot merged commit 08c4b9b into quantumlib:master Mar 12, 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 Mar 12, 2021
@vtomole vtomole mentioned this pull request Mar 16, 2021
@daxfohl
Copy link
Contributor

daxfohl commented Apr 24, 2021

Over in qiskit they mention Hidden Shift can be represented by a diagonal gate https://qiskit.org/documentation/stubs/qiskit.circuit.library.Diagonal.html, but I'm not grokking. We have a hidden shift example https://github.com/quantumlib/Cirq/blob/master/examples/hidden_shift_algorithm.py. Should it be updated to use this (or perhaps Hamiltonian Binary since you mention they have the same underlying math)?

@bichengying
Copy link
Collaborator Author

Over in qiskit they mention Hidden Shift can be represented by a diagonal gate https://qiskit.org/documentation/stubs/qiskit.circuit.library.Diagonal.html, but I'm not grokking. We have a hidden shift example https://github.com/quantumlib/Cirq/blob/master/examples/hidden_shift_algorithm.py. Should it be updated to use this (or perhaps Hamiltonian Binary since you mention they have the same underlying math)?

Hi Dax,

Right, simulating any oracle problem, like f(x) |x>, is equivalent to a 2^n * 2^n diagonal unitary matrix times a 2^n vector. In this point of view, yes we can use a diagonal gate in the Hidden Shift example. However, in this example, I think it is ok to leave it as is mainly because the oracle function consider in the example is simply

def make_oracle_f(qubits):
    """Implement function {f(x) = Σ_i x_(2i) x_(2i+1)}."""
    return [cirq.CZ(qubits[2 * i], qubits[2 * i + 1]) for i in range(len(qubits) // 2)]

Note CZ itself is already a diagonal gate. This is clean and good for example illustration. The benefit of using a diagonal gate will exhibit when we want to use other more complicated functions that we know f(x) but don't know how to write into existing primary gates easily. Similar opinion on Hamiltonian Binary. (I.e. if we know the oracle function boolean expression.)

Qiskit Diagonal gate has more functionalities (we have dense unitary and 1, 2-qubits decomposite representation only now). We can also consider making it more generalized first.

@bichengying bichengying deleted the DiagonalGate branch May 16, 2021 01:29
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants