Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `E` command in patterns.
- The pretty-printing (`repr`) of instructions and commands is updated to use notation without parentheses: `repr(E((0, 1))) == "E(0, 1)"`.

- #694: CH gate is now supported (its support was missing as it is a standard OpenQASM gate).
- #624: CH gate is now supported (its support was missing as it is a standard OpenQASM gate).

- #637: Decompositions no longer drop global phases, which made controlled gates produced by `insert_control` wrong. `instruction_to_jcz` now keeps the global phase of `S`, `SDG`, `T`, `TDG`, `SX`, `SXDG`, `X`, `Y` and `Z`, and `decompose_ccx` now uses `T`, `TDG` and `S` gates as in Nielsen & Chuang instead of `RZ` rotations.

### Changed

Expand Down
38 changes: 24 additions & 14 deletions graphix/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1205,8 +1205,8 @@ def decompose_rzz(instr: Instruction.RZZ) -> Iterator[Instruction.CNOT | Instruc

def decompose_ccx(
instr: Instruction.CCX,
) -> Iterator[Instruction.H | Instruction.CNOT | Instruction.RZ]:
"""Yield a decomposition of the CCX gate into H, CNOT, T and T-dagger gates.
) -> Iterator[Instruction.H | Instruction.CNOT | Instruction.T | Instruction.TDG | Instruction.S]:
"""Yield a decomposition of the CCX gate into H, CNOT, T, T-dagger and S gates.

This decomposition of the Toffoli gate can be found in
Michael A. Nielsen and Isaac L. Chuang,
Expand All @@ -1226,20 +1226,20 @@ def decompose_ccx(
c0, c1, t = instr.controls[0], instr.controls[1], instr.target
yield Instruction.H(t)
yield Instruction.CNOT(control=c1, target=t)
yield Instruction.RZ(t, -ANGLE_PI / 4)
yield Instruction.TDG(t)
yield Instruction.CNOT(control=c0, target=t)
yield Instruction.RZ(t, ANGLE_PI / 4)
yield Instruction.T(t)
yield Instruction.CNOT(control=c1, target=t)
yield Instruction.RZ(t, -ANGLE_PI / 4)
yield Instruction.TDG(t)
yield Instruction.CNOT(control=c0, target=t)
yield Instruction.RZ(c1, -ANGLE_PI / 4)
yield Instruction.RZ(t, ANGLE_PI / 4)
yield Instruction.TDG(c1)
yield Instruction.T(t)
yield Instruction.CNOT(control=c0, target=c1)
yield Instruction.H(t)
yield Instruction.RZ(c1, -ANGLE_PI / 4)
yield Instruction.TDG(c1)
yield Instruction.CNOT(control=c0, target=c1)
yield Instruction.RZ(c0, ANGLE_PI / 4)
yield Instruction.RZ(c1, ANGLE_PI / 2)
yield Instruction.T(c0)
yield Instruction.S(c1)


def decompose_cnot(instr: Instruction.CNOT) -> Iterator[Instruction.H | Instruction.CZ]:
Expand Down Expand Up @@ -1560,24 +1560,34 @@ def instruction_to_jcz(
return
case InstructionKind.H:
yield Instruction.J(instr.target, 0)
# Global phases are kept so that the decomposition remains
# correct when it is controlled (see `insert_control`).
case InstructionKind.S:
yield from decompose_rz(Instruction.RZ(instr.target, ANGLE_PI / 2))
yield from instructions_to_jcz(decompose_p(Instruction.P(instr.target, ANGLE_PI / 2)))
case InstructionKind.SDG:
yield from decompose_rz(Instruction.RZ(instr.target, -ANGLE_PI / 2))
yield from instructions_to_jcz(decompose_p(Instruction.P(instr.target, -ANGLE_PI / 2)))
case InstructionKind.T:
yield from decompose_rz(Instruction.RZ(instr.target, ANGLE_PI / 4))
yield from instructions_to_jcz(decompose_p(Instruction.P(instr.target, ANGLE_PI / 4)))
case InstructionKind.TDG:
yield from decompose_rz(Instruction.RZ(instr.target, -ANGLE_PI / 4))
yield from instructions_to_jcz(decompose_p(Instruction.P(instr.target, -ANGLE_PI / 4)))
case InstructionKind.SX:
# SX = e^{iπ/4} Rx(π/2)
yield from decompose_rx(Instruction.RX(instr.target, ANGLE_PI / 2))
yield Instruction.GPHASE(ANGLE_PI / 4)
case InstructionKind.SXDG:
# SXDG = e^{-iπ/4} Rx(-π/2)
yield from decompose_rx(Instruction.RX(instr.target, -ANGLE_PI / 2))
yield Instruction.GPHASE(-ANGLE_PI / 4)
case InstructionKind.X:
# X = i Rx(π)
yield from decompose_rx(Instruction.RX(instr.target, ANGLE_PI))
yield Instruction.GPHASE(ANGLE_PI / 2)
case InstructionKind.Y:
yield from instructions_to_jcz(decompose_y(instr))
case InstructionKind.Z:
# Z = i Rz(π)
yield from decompose_rz(Instruction.RZ(instr.target, ANGLE_PI))
yield Instruction.GPHASE(ANGLE_PI / 2)
case InstructionKind.RX:
yield from decompose_rx(instr)
case InstructionKind.RY:
Expand Down
90 changes: 90 additions & 0 deletions tests/test_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from graphix.branch_selector import ConstBranchSelector, FixedBranchSelector
from graphix.fundamentals import ANGLE_PI, Axis, Plane, Sign
from graphix.instruction import I, InstructionKind
from graphix.ops import Ops, controlled
from graphix.random_objects import rand_circuit, rand_gate, rand_state_vector
from graphix.sim.density_matrix import DensityMatrix
from graphix.sim.statevec import Statevector, StatevectorBackend
Expand All @@ -37,6 +38,8 @@
from collections.abc import Iterable, Iterator
from typing import Literal

import numpy.typing as npt

from graphix.instruction import InstructionType, InstructionTypeWithoutM
from graphix.measurements import Outcome
from graphix.states import State
Expand Down Expand Up @@ -1013,6 +1016,93 @@ def test_cr() -> None:
]


@pytest.mark.parametrize(
("instrs", "u"),
[
([Instruction.H(1)], Ops.H),
([Instruction.S(1)], Ops.S),
([Instruction.SDG(1)], Ops.SDG),
([Instruction.T(1)], Ops.T),
([Instruction.TDG(1)], Ops.TDG),
([Instruction.SX(1)], Ops.SX),
([Instruction.SXDG(1)], Ops.SXDG),
([Instruction.CCX((1, 2), 3)], Ops.CCX),
([Instruction.CSWAP(1, (2, 3))], Ops.CSWAP),
(list(instruction_to_jcz(Instruction.X(1))), Ops.X),
(list(instruction_to_jcz(Instruction.Y(1))), Ops.Y),
(list(instruction_to_jcz(Instruction.Z(1))), Ops.Z),
(list(decompose_ccx(Instruction.CCX((1, 2), 3))), Ops.CCX),
],
)
def test_insert_control_global_phase(instrs: list[InstructionTypeWithoutM], u: npt.NDArray[np.complex128]) -> None:
"""Check that `insert_control` is exact when the control is in superposition (#637)."""
n = int(np.log2(len(u))) + 1
state = Circuit(n, instr=insert_control(0, instrs)).simulate(input_state=BasicStates.PLUS).state
assert isinstance(state, Statevector)
expected = controlled(u) @ Statevector(nqubit=n, data=BasicStates.PLUS).flatten().astype(np.complex128)
assert np.allclose(state.flatten(), expected)


@pytest.mark.parametrize(
("instr", "u"),
[
(Instruction.I(1), Ops.I),
(Instruction.X(1), Ops.X),
(Instruction.Y(1), Ops.Y),
(Instruction.Z(1), Ops.Z),
(Instruction.H(1), Ops.H),
(Instruction.S(1), Ops.S),
(Instruction.SDG(1), Ops.SDG),
(Instruction.T(1), Ops.T),
(Instruction.TDG(1), Ops.TDG),
(Instruction.SX(1), Ops.SX),
(Instruction.SXDG(1), Ops.SXDG),
(Instruction.J(1, ANGLE_PI / 3), Ops.j(ANGLE_PI / 3)),
(Instruction.P(1, ANGLE_PI / 3), Ops.p(ANGLE_PI / 3)),
(Instruction.RX(1, ANGLE_PI / 3), Ops.rx(ANGLE_PI / 3)),
(Instruction.RY(1, ANGLE_PI / 3), Ops.ry(ANGLE_PI / 3)),
(Instruction.RZ(1, ANGLE_PI / 3), Ops.rz(ANGLE_PI / 3)),
(Instruction.U(1, ANGLE_PI / 3, ANGLE_PI / 5, ANGLE_PI / 7), Ops.u(ANGLE_PI / 3, ANGLE_PI / 5, ANGLE_PI / 7)),
(Instruction.CNOT(1, 2), Ops.CNOT),
(Instruction.CY(1, 2), Ops.CY),
(Instruction.CZ(1, 2), Ops.CZ),
(Instruction.CH(1, 2), Ops.CH),
(Instruction.CJ(1, 2, ANGLE_PI / 3), Ops.cj(ANGLE_PI / 3)),
(Instruction.CP(1, 2, ANGLE_PI / 3), Ops.cp(ANGLE_PI / 3)),
(Instruction.CRX(1, 2, ANGLE_PI / 3), Ops.crx(ANGLE_PI / 3)),
(Instruction.CRY(1, 2, ANGLE_PI / 3), Ops.cry(ANGLE_PI / 3)),
(Instruction.CRZ(1, 2, ANGLE_PI / 3), Ops.crz(ANGLE_PI / 3)),
(
Instruction.CU(1, 2, ANGLE_PI / 3, ANGLE_PI / 5, ANGLE_PI / 7, ANGLE_PI / 9),
Ops.cu(ANGLE_PI / 3, ANGLE_PI / 5, ANGLE_PI / 7, ANGLE_PI / 9),
),
(Instruction.RZZ(1, 2, ANGLE_PI / 3), Ops.rzz(ANGLE_PI / 3)),
(Instruction.SWAP(1, 2), Ops.SWAP),
(Instruction.CCX((1, 2), 3), Ops.CCX),
(Instruction.CSWAP(1, (2, 3)), Ops.CSWAP),
],
)
def test_controlled_jcz_global_phase(
instr: InstructionTypeWithoutM, u: npt.NDArray[np.complex128], fx_rng: Generator
) -> None:
"""Check that the J-∧z decomposition of `instr` keeps the global phase (#637).

The statevector simulator ignores top-level `GPHASE` instructions,
so a global phase cannot be observed by simulating the
decomposition alone: it only becomes a relative phase, and thus
observable, once the decomposition is controlled. We therefore
control the decomposition with qubit 0 and compare the result,
on a random input state, with the exact controlled gate
`controlled(u)`, where `u` is the matrix of `instr` acting on
qubits 1, 2, ...
"""
n = int(np.log2(len(u))) + 1
input_state = rand_state_vector(n, rng=fx_rng)
state = Circuit(n, instr=insert_control(0, instruction_to_jcz(instr))).simulate(input_state=input_state).state
assert isinstance(state, Statevector)
assert np.allclose(state.flatten(), controlled(u) @ input_state)


def test_condinstr_to_jcz() -> None:
with pytest.raises(NotImplementedError):
next(instruction_to_jcz(Instruction.CONDINSTR({1}, (Instruction.H(0),))))
Expand Down
Loading