-
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 5 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 |
|---|---|---|
|
|
@@ -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)) | ||
| qubits = gate.get_qubit_operands() | ||
|
|
||
| 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,40 @@ 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 qubit in qubits: | ||
| circuit.phase_map.add_qubit_phase(qubit, euler_phase) | ||
|
|
||
| if len(qubits) > 1: | ||
| relative_phase = float( | ||
| np.real(circuit.phase_map.get_qubit_phase(qubits[0]) - circuit.phase_map.get_qubit_phase(qubits[1])) | ||
| ) | ||
| if abs(relative_phase) > ATOL: | ||
| list(replacement_gates).append(Rz(qubits[0], Float(relative_phase))) | ||
|
|
||
| return list(replacement_gates) | ||
|
|
||
|
|
||
| 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 +109,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,20 @@ | ||||||||||||||||||||||
| import numpy as np | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| from opensquirrel.ir import Qubit, QubitLike | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| class PhaseMap: | ||||||||||||||||||||||
|
||||||||||||||||||||||
| def __init__(self, qubit_register_size: int) -> None: | ||||||||||||||||||||||
| """Initialize a PhaseMap object.""" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| self.qubit_phase_map = np.zeros(qubit_register_size, dtype=np.float64) | ||||||||||||||||||||||
|
||||||||||||||||||||||
| self.qubit_phase_map = np.zeros(qubit_register_size, dtype=np.float64) | |
| self.qubit_phase_map = [0] * qubit_register_size |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def add_qubit_phase(self, qubit: QubitLike, phase: np.float64) -> None: | |
| self.qubit_phase_map[Qubit(qubit).index] += phase | |
| def get_qubit_phase(self, qubit: QubitLike) -> np.float64: | |
| return np.float64(self.qubit_phase_map[Qubit(qubit).index]) | |
| def add_qubit_phase(self, qubit: QubitLike, phase: float) -> None: | |
| self.qubit_phase_map[Qubit(qubit).index] += phase | |
| def get_qubit_phase(self, qubit: QubitLike) -> float: | |
| return self.qubit_phase_map[Qubit(qubit).index] |
| 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] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import math | ||
|
|
||
| import numpy as np | ||
|
|
||
| from opensquirrel import Circuit, CircuitBuilder | ||
| from opensquirrel.passes.decomposer import CNOT2CZDecomposer, CNOTDecomposer, McKayDecomposer, XYXDecomposer | ||
| from opensquirrel.passes.merger import SingleQubitGatesMerger | ||
|
|
||
|
|
||
| def test_integration_global_phase() -> None: | ||
|
||
| 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] | ||
| """ | ||
| ) | ||
|
|
||
|
|
||
| def test_phase_map_circuit_builder() -> None: | ||
|
||
| circuit_builder = CircuitBuilder(4) | ||
| circuit_builder.H(0).H(1).H(2).H(3).Rz(0, math.pi / 8).Ry(1, math.pi / 4).Rx(2, 3 * math.pi / 8).Ry( | ||
| 3, math.pi / 2 | ||
| ).CNOT(1, 2).CNOT(0, 3).Rz(0, math.pi / 8).Ry(1, math.pi / 4).Rx(2, 3 * math.pi / 8).Ry(3, math.pi / 2) | ||
| circuit = circuit_builder.to_circuit() | ||
| circuit.merge(SingleQubitGatesMerger()) | ||
| circuit.decompose(XYXDecomposer()) | ||
| assert np.allclose(circuit.phase_map.qubit_phase_map, [math.pi / 2, math.pi / 2, -math.pi / 2, math.pi / 2]) | ||
Uh oh!
There was an error while loading. Please reload this page.