-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add support for allocating qubits in decompose to cirq.unitary #6112
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
Merged
tanujkhattar
merged 13 commits into
quantumlib:master
from
NoureldinYosri:unitary_with_ancillas
Jun 5, 2023
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
af8a5dd
Add support for allocating qubits in decompose to cirq.unitary
NoureldinYosri d656a38
Merge branch 'master' into unitary_with_ancillas
NoureldinYosri d10ae04
fixed apply_unitaries
NoureldinYosri 4326084
fix mypy
NoureldinYosri cb194ee
Merge branch 'master' into unitary_with_ancillas
NoureldinYosri 5f3e52d
refactored tests
NoureldinYosri 6fdc6e1
Merge branch 'master' into unitary_with_ancillas
NoureldinYosri c7fd8f7
addressing comments
NoureldinYosri 88f7f2d
added sample_gates_test.py
NoureldinYosri 14dd571
Improved sample_gates.py implementation and unitary_protocol tests. A…
tanujkhattar d32b225
fixed lint
NoureldinYosri ff03e6c
retrigger checks
NoureldinYosri 7617bbd
fix coverage
NoureldinYosri 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,79 @@ | ||
# Copyright 2023 The Cirq Developers | ||
# | ||
# 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 dataclasses | ||
|
||
import cirq | ||
import numpy as np | ||
from cirq import ops, qis | ||
|
||
|
||
def _matrix_for_phasing_state(num_qubits, phase_state, phase): | ||
matrix = qis.eye_tensor((2,) * num_qubits, dtype=np.complex128) | ||
matrix = matrix.reshape((2**num_qubits, 2**num_qubits)) | ||
matrix[phase_state, phase_state] = phase | ||
print(num_qubits, phase_state, phase) | ||
print(matrix) | ||
return matrix | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class PhaseUsingCleanAncilla(ops.Gate): | ||
r"""Phases the state $|phase_state>$ by $\exp(1j * \pi * \theta)$ using one clean ancilla.""" | ||
|
||
theta: float | ||
phase_state: int = 1 | ||
target_bitsize: int = 1 | ||
ancilla_bitsize: int = 1 | ||
|
||
def _num_qubits_(self): | ||
return self.target_bitsize | ||
|
||
def _decompose_(self, qubits): | ||
anc = ops.NamedQubit.range(self.ancilla_bitsize, prefix="anc") | ||
cv = [int(x) for x in f'{self.phase_state:0{self.target_bitsize}b}'] | ||
cnot_ladder = [cirq.CNOT(anc[i - 1], anc[i]) for i in range(1, self.ancilla_bitsize)] | ||
|
||
yield ops.X(anc[0]).controlled_by(*qubits, control_values=cv) | ||
yield [cnot_ladder, ops.Z(anc[-1]) ** self.theta, reversed(cnot_ladder)] | ||
yield ops.X(anc[0]).controlled_by(*qubits, control_values=cv) | ||
|
||
def narrow_unitary(self) -> np.ndarray: | ||
"""Narrowed unitary corresponding to the unitary effect applied on target qubits.""" | ||
phase = np.exp(1j * np.pi * self.theta) | ||
return _matrix_for_phasing_state(self.target_bitsize, self.phase_state, phase) | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class PhaseUsingDirtyAncilla(ops.Gate): | ||
r"""Phases the state $|phase_state>$ by -1 using one dirty ancilla.""" | ||
|
||
phase_state: int = 1 | ||
target_bitsize: int = 1 | ||
ancilla_bitsize: int = 1 | ||
|
||
def _num_qubits_(self): | ||
return self.target_bitsize | ||
|
||
def _decompose_(self, qubits): | ||
anc = ops.NamedQubit.range(self.ancilla_bitsize, prefix="anc") | ||
cv = [int(x) for x in f'{self.phase_state:0{self.target_bitsize}b}'] | ||
cnot_ladder = [cirq.CNOT(anc[i - 1], anc[i]) for i in range(1, self.ancilla_bitsize)] | ||
yield ops.X(anc[0]).controlled_by(*qubits, control_values=cv) | ||
yield [cnot_ladder, ops.Z(anc[-1]), reversed(cnot_ladder)] | ||
yield ops.X(anc[0]).controlled_by(*qubits, control_values=cv) | ||
yield [cnot_ladder, ops.Z(anc[-1]), reversed(cnot_ladder)] | ||
|
||
def narrow_unitary(self) -> np.ndarray: | ||
"""Narrowed unitary corresponding to the unitary effect applied on target qubits.""" | ||
return _matrix_for_phasing_state(self.target_bitsize, self.phase_state, -1) |
This file contains hidden or 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,59 @@ | ||
# Copyright 2023 The Cirq Developers | ||
# | ||
# 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 pytest | ||
|
||
import numpy as np | ||
from cirq.testing import sample_gates | ||
import cirq | ||
|
||
|
||
@pytest.mark.parametrize('theta', np.linspace(0, 2 * np.pi, 20)) | ||
def test_phase_using_clean_ancilla(theta: float): | ||
g = sample_gates.PhaseUsingCleanAncilla(theta) | ||
q = cirq.LineQubit(0) | ||
qubit_order = cirq.QubitOrder.explicit([q], fallback=cirq.QubitOrder.DEFAULT) | ||
decomposed_unitary = cirq.Circuit(cirq.decompose_once(g.on(q))).unitary(qubit_order=qubit_order) | ||
phase = np.exp(1j * np.pi * theta) | ||
np.testing.assert_allclose(g.narrow_unitary(), np.array([[1, 0], [0, phase]])) | ||
np.testing.assert_allclose( | ||
decomposed_unitary, | ||
# fmt: off | ||
np.array( | ||
[ | ||
[1 , 0 , 0 , 0], | ||
[0 , phase, 0 , 0], | ||
[0 , 0 , phase, 0], | ||
[0 , 0 , 0 , 1], | ||
] | ||
), | ||
# fmt: on | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'target_bitsize, phase_state', [(1, 0), (1, 1), (2, 0), (2, 1), (2, 2), (2, 3)] | ||
) | ||
@pytest.mark.parametrize('ancilla_bitsize', [1, 4]) | ||
def test_phase_using_dirty_ancilla(target_bitsize, phase_state, ancilla_bitsize): | ||
g = sample_gates.PhaseUsingDirtyAncilla(phase_state, target_bitsize, ancilla_bitsize) | ||
q = cirq.LineQubit.range(target_bitsize) | ||
qubit_order = cirq.QubitOrder.explicit(q, fallback=cirq.QubitOrder.DEFAULT) | ||
decomposed_circuit = cirq.Circuit(cirq.decompose_once(g.on(*q))) | ||
decomposed_unitary = decomposed_circuit.unitary(qubit_order=qubit_order) | ||
phase_matrix = np.eye(2**target_bitsize) | ||
phase_matrix[phase_state, phase_state] = -1 | ||
np.testing.assert_allclose(g.narrow_unitary(), phase_matrix) | ||
np.testing.assert_allclose( | ||
decomposed_unitary, np.kron(phase_matrix, np.eye(2**ancilla_bitsize)), atol=1e-5 | ||
) |
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.
Uh oh!
There was an error while loading. Please reload this page.