-
Notifications
You must be signed in to change notification settings - Fork 4
[CQT-243] Implementation of global phase #563
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
base: develop
Are you sure you want to change the base?
Changes from 2 commits
d0b0d2e
c0d4f62
92bf58c
c91d0eb
4c0a429
04208c6
25c94f3
25684a3
e58095b
14499b8
b93d584
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ def are_matrices_equivalent_up_to_global_phase( | |
| Returns: | ||
| Whether two matrices are equivalent up to a global phase. | ||
| """ | ||
|
|
||
| first_non_zero = next( | ||
| (i, j) for i in range(matrix_a.shape[0]) for j in range(matrix_a.shape[1]) if abs(matrix_a[i, j]) > ATOL | ||
| ) | ||
|
|
@@ -51,6 +52,24 @@ def are_matrices_equivalent_up_to_global_phase( | |
| return np.allclose(matrix_a, phase_difference * matrix_b, atol=ATOL) | ||
|
|
||
|
|
||
| def calculate_phase_difference(matrix_a: NDArray[np.complex128], matrix_b: NDArray[np.complex128]) -> np.complex128: | ||
| """Calculates the phase difference between two matrices. | ||
| Args: | ||
| matrix_a: first matrix. | ||
| matrix_b: second matrix. | ||
| Returns: | ||
| The phase difference between the two matrices. | ||
| """ | ||
| first_non_zero = next( | ||
| (i, j) for i in range(matrix_a.shape[0]) for j in range(matrix_a.shape[1]) if abs(matrix_a[i, j]) > ATOL | ||
| ) | ||
|
|
||
| if abs(matrix_b[first_non_zero]) < ATOL: | ||
| return np.complex128(1) | ||
|
|
||
| return np.complex128(matrix_a[first_non_zero] / matrix_b[first_non_zero]) | ||
|
|
||
|
|
||
| def is_identity_matrix_up_to_a_global_phase(matrix: NDArray[np.complex128]) -> bool: | ||
| """Checks whether matrix is an identity matrix up to a global phase. | ||
|
|
||
|
|
@@ -60,3 +79,13 @@ def is_identity_matrix_up_to_a_global_phase(matrix: NDArray[np.complex128]) -> b | |
| Whether matrix is an identity matrix up to a global phase. | ||
| """ | ||
| return are_matrices_equivalent_up_to_global_phase(matrix, np.eye(matrix.shape[0], dtype=np.complex128)) | ||
|
|
||
|
|
||
| def get_phase_angle(scalar: np.complex128) -> np.complex128: | ||
|
||
| """Derives the Euler rotation angle from a scalar. | ||
| Args: | ||
| scalar: scalar to convert. | ||
| Returns: | ||
| Euler phase angle of scalar. | ||
| """ | ||
| return np.complex128(-1j * np.log(scalar)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,14 +2,25 @@ | |
|
|
||
| from abc import ABC, abstractmethod | ||
| from collections.abc import Callable, Iterable | ||
| from typing import Any | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| import numpy as np | ||
|
|
||
| from opensquirrel.circuit_matrix_calculator import get_circuit_matrix | ||
| from opensquirrel.common import are_matrices_equivalent_up_to_global_phase, is_identity_matrix_up_to_a_global_phase | ||
| from opensquirrel.common import ( | ||
| ATOL, | ||
| are_matrices_equivalent_up_to_global_phase, | ||
| calculate_phase_difference, | ||
| get_phase_angle, | ||
| is_identity_matrix_up_to_a_global_phase, | ||
| ) | ||
| from opensquirrel.default_instructions import is_anonymous_gate | ||
| from opensquirrel.ir import IR, Gate | ||
| from opensquirrel.ir import Float, Gate, Rz | ||
| from opensquirrel.reindexer import get_reindexed_circuit | ||
|
|
||
| if TYPE_CHECKING: | ||
| from opensquirrel.circuit import Circuit | ||
|
|
||
|
|
||
| class Decomposer(ABC): | ||
| def __init__(self, **kwargs: Any) -> None: ... | ||
|
|
@@ -19,13 +30,23 @@ def decompose(self, gate: Gate) -> list[Gate]: | |
| raise NotImplementedError() | ||
|
|
||
|
|
||
| def check_gate_replacement(gate: Gate, replacement_gates: Iterable[Gate]) -> None: | ||
| def check_gate_replacement(gate: Gate, replacement_gates: Iterable[Gate], circuit: Circuit | None = None) -> list[Gate]: | ||
| """ | ||
| Verifies the replacement gates against the given gate. | ||
| Args: | ||
| gate: original gate | ||
| replacement_gates: gates replacing the gate | ||
| circuit: circuit to verify | ||
| Returns: | ||
| Returns verified list of replacement gates with possible correction. | ||
| """ | ||
| gate_qubit_indices = [q.index for q in gate.get_qubit_operands()] | ||
| replacement_gates_qubit_indices = set() | ||
| replaced_matrix = get_circuit_matrix(get_reindexed_circuit([gate], gate_qubit_indices)) | ||
| qubit_list = gate.get_qubit_operands() | ||
juanboschero marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if is_identity_matrix_up_to_a_global_phase(replaced_matrix): | ||
| return | ||
| return [] | ||
|
|
||
| for g in replacement_gates: | ||
| replacement_gates_qubit_indices.update([q.index for q in g.get_qubit_operands()]) | ||
|
|
@@ -40,25 +61,42 @@ def check_gate_replacement(gate: Gate, replacement_gates: Iterable[Gate]) -> Non | |
| msg = f"replacement for gate {gate.name} does not preserve the quantum state" | ||
| raise ValueError(msg) | ||
|
|
||
| if circuit is not None: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How could Is this because the replacement functionality can also be used on a single gate? If so, I'm tempted to remove that option. Or at least not have the replacer be part of the |
||
| phase_difference = calculate_phase_difference(replaced_matrix, replacement_matrix) | ||
| euler_phase = get_phase_angle(phase_difference) | ||
| for q in gate.get_qubit_operands(): | ||
| circuit.phase_map.add_qubit_phase(q, euler_phase) | ||
|
||
|
|
||
| if len(gate_qubit_indices) > 1: | ||
| relative_phase = float( | ||
| np.real( | ||
| circuit.phase_map.get_qubit_phase(qubit_list[1]) - circuit.phase_map.get_qubit_phase(qubit_list[0]) | ||
| ) | ||
| ) | ||
| if abs(relative_phase) > ATOL: | ||
| list(replacement_gates).append(Rz(gate.get_qubit_operands()[0], Float(-1 * relative_phase))) | ||
|
|
||
| return list(replacement_gates) | ||
juanboschero marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def decompose(ir: IR, decomposer: Decomposer) -> None: | ||
| def decompose(circuit: Circuit, decomposer: Decomposer) -> None: | ||
| """Applies `decomposer` to every gate in the circuit, replacing each gate by the output of `decomposer`. | ||
| When `decomposer` decides to not decomposer a gate, it needs to return a list with the intact gate as single | ||
| element. | ||
| """ | ||
| statement_index = 0 | ||
| while statement_index < len(ir.statements): | ||
| statement = ir.statements[statement_index] | ||
| while statement_index < len(circuit.ir.statements): | ||
| statement = circuit.ir.statements[statement_index] | ||
|
|
||
| if not isinstance(statement, Gate): | ||
| statement_index += 1 | ||
| continue | ||
|
|
||
| gate = statement | ||
| replacement_gates: list[Gate] = decomposer.decompose(statement) | ||
| check_gate_replacement(gate, replacement_gates) | ||
| replacement_gates = check_gate_replacement(gate, replacement_gates, circuit) | ||
|
|
||
| ir.statements[statement_index : statement_index + 1] = replacement_gates | ||
| circuit.ir.statements[statement_index : statement_index + 1] = replacement_gates | ||
| statement_index += len(replacement_gates) | ||
|
|
||
|
|
||
|
|
@@ -73,8 +111,8 @@ def decompose(self, gate: Gate) -> list[Gate]: | |
| return self.replacement_gates_function(*gate.arguments) | ||
|
|
||
|
|
||
| def replace(ir: IR, gate: type[Gate], replacement_gates_function: Callable[..., list[Gate]]) -> None: | ||
| def replace(circuit: Circuit, gate: type[Gate], replacement_gates_function: Callable[..., list[Gate]]) -> None: | ||
| """Does the same as decomposer, but only applies to a given gate.""" | ||
| generic_replacer = _GenericReplacer(gate, replacement_gates_function) | ||
|
|
||
| decompose(ir, generic_replacer) | ||
| decompose(circuit, generic_replacer) | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||
| import numpy as np | ||||||
| from numpy.typing import NDArray | ||||||
|
|
||||||
| from opensquirrel.ir import QubitLike | ||||||
|
|
||||||
|
|
||||||
| class PhaseMap: | ||||||
| def __init__(self, phase_map: NDArray[np.complex128]) -> None: | ||||||
elenbaasc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| """Initialize a PhaseMap object.""" | ||||||
| self.qubit_phase_map = phase_map | ||||||
|
|
||||||
| def __contains__(self, qubit: QubitLike) -> bool: | ||||||
| """Checks if qubit is in the phase map.""" | ||||||
| return qubit in self.qubit_phase_map | ||||||
|
|
||||||
| def add_qubit_phase(self, qubit: QubitLike, phase: np.complex128) -> None: | ||||||
|
||||||
| def add_qubit_phase(self, qubit: QubitLike, phase: np.complex128) -> None: | |
| def set_qubit_phase(self, qubit: QubitLike, phase: np.complex128) -> None: |
elenbaasc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,9 +123,11 @@ def test_spin2plus_backend() -> None: | |
| X90 q[0] | ||
| Rz(-1.5707964) q[0] | ||
| b[0] = measure q[0] | ||
| Rz(2.3561946) q[1] | ||
| Rz(0.78539824) q[1] | ||
| X90 q[1] | ||
| Rz(-3.1415927) q[1] | ||
| Rz(1.5707963) q[1] | ||
| X90 q[1] | ||
| Rz(1.5707963) q[1] | ||
|
||
| b[2] = measure q[1] | ||
| Rz(1.5707963) q[1] | ||
| X90 q[1] | ||
|
|
@@ -780,3 +782,60 @@ def test_rydberg_backend() -> None: | |
| X q[8] | ||
| """ | ||
| ) | ||
|
|
||
|
|
||
| def test_integration_global_phase() -> None: | ||
elenbaasc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| circuit = Circuit.from_string( | ||
| """ | ||
| version 3.0 | ||
| qubit[3] q | ||
| H q[0:2] | ||
| Ry(1.5789) q[0] | ||
| H q[0] | ||
| CNOT q[1], q[0] | ||
| Ry(3.09) q[0] | ||
| Ry(0.318) q[1] | ||
| Ry(0.18) q[2] | ||
| CNOT q[2], q[0] | ||
| """, | ||
| ) | ||
|
|
||
| # Decompose 2-qubit gates to a decomposition where the 2-qubit interactions are captured by CNOT gates | ||
| circuit.decompose(decomposer=CNOTDecomposer()) | ||
|
|
||
| # Replace CNOT gates with CZ gates | ||
| circuit.decompose(decomposer=CNOT2CZDecomposer()) | ||
| # Merge single-qubit gates and decompose with McKay decomposition. | ||
| circuit.merge(merger=SingleQubitGatesMerger()) | ||
| circuit.decompose(decomposer=McKayDecomposer()) | ||
| assert ( | ||
| str(circuit) | ||
| == """version 3.0 | ||
|
|
||
| qubit[3] q | ||
|
|
||
| Rz(1.5707963) q[1] | ||
| X90 q[1] | ||
| Rz(1.5707963) q[1] | ||
| Rz(3.1415927) q[0] | ||
| X90 q[0] | ||
| Rz(0.0081036221) q[0] | ||
| X90 q[0] | ||
| CZ q[1], q[0] | ||
| X90 q[2] | ||
| Rz(1.3907963) q[2] | ||
| X90 q[2] | ||
| Rz(3.1415927) q[0] | ||
| X90 q[0] | ||
| Rz(0.051592654) q[0] | ||
| X90 q[0] | ||
| CZ q[2], q[0] | ||
| Rz(-1.5707963) q[0] | ||
| X90 q[0] | ||
| Rz(1.5707963) q[0] | ||
| Rz(3.1415927) q[1] | ||
| X90 q[1] | ||
| Rz(2.8235927) q[1] | ||
| X90 q[1] | ||
| """ | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.