Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion ppvm-python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors = [
]
requires-python = ">=3.10"
dependencies = [
"bloqade-circuit>=0.14.1",
"bloqade-circuit>=0.15.0",
"kirin-toolchain~=0.22.2",
"numpy>=2.2.6",
]
Expand Down
30 changes: 30 additions & 0 deletions ppvm-python/src/ppvm/squin_interpreter/impls/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ def control(
for ctrl, target in zip(controls, targets):
method(ctrl.addr, target.addr)

@interp.impl(gate.stmts.CCZ)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Might be worth adding native support for this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks david! Given the approval, I will proceed to merge it so that it does not block the bloqade-qec work, and I will make another PR for native support.

def ccz(
self,
interp_: GeneralizedTableauInterpreter,
frame: interp.Frame,
stmt: gate.stmts.CCZ,
):
"""Apply CCZ using seven T/T-adjoint gates and six CNOTs."""
controls1: ilist.IList[GeneralizedTableauQubit, Any] = frame.get(stmt.controls1)
controls2: ilist.IList[GeneralizedTableauQubit, Any] = frame.get(stmt.controls2)
targets: ilist.IList[GeneralizedTableauQubit, Any] = frame.get(stmt.targets)
backend = interp_.backend

for control1, control2, target in zip(controls1, controls2, targets):
a, b, c = control1.addr, control2.addr, target.addr
if any(backend.is_lost(q) or backend.is_leaked(q) for q in (a, b, c)):
continue
# 4abc = a + b + c - (a^b) - (a^c) - (b^c) + (a^b^c).
backend.t(a, b, c)
backend.cnot(a, b)
backend.t_dag(b)
backend.cnot(a, b)
backend.cnot(a, c)
backend.t_dag(c)
backend.cnot(b, c)
backend.t(c)
backend.cnot(a, c)
backend.t_dag(c)
backend.cnot(b, c)

@interp.impl(gate.stmts.U3)
def u3(
self,
Expand Down
58 changes: 58 additions & 0 deletions ppvm-python/test/generalized_tableau/test_simulator_device.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import math

import pytest
from bloqade import squin
from bloqade.decoders.dialects.annotate.types import MeasurementResultValue
from kirin.dialects import ilist
Expand Down Expand Up @@ -199,6 +200,63 @@ def main():
assert _run_kernel(main, 6) == [ONE, ONE, ONE, ZERO, ONE, ONE]


def test_broadcast_ccz_as_toffoli():
@squin.kernel
def main():
q = squin.qalloc(12)
a = q[:4]
b = q[4:8]
target = q[8:12]

# The four control pairs are 00, 01, 10, 11.
squin.x(a[2])
squin.x(a[3])
squin.x(b[1])
squin.x(b[3])
squin.broadcast.h(target)
squin.broadcast.ccz(a, b, target)
squin.broadcast.h(target)
return squin.broadcast.measure(q)

result = _run_kernel(main, 12)
assert result[:4] == [ZERO, ZERO, ONE, ONE] # a
assert result[4:8] == [ZERO, ONE, ZERO, ONE] # b
assert result[8:12] == [ZERO, ZERO, ZERO, ONE] # a AND b


def test_ccz_changes_the_relative_phase_of_ghz():
@squin.kernel
def main():
q = squin.qalloc(3)
squin.h(q[0])
squin.cx(q[0], q[1])
squin.cx(q[0], q[2])
squin.ccz(q[0], q[1], q[2])
# Undo GHZ preparation: its minus sign becomes a deterministic one.
squin.cx(q[0], q[2])
squin.cx(q[0], q[1])
squin.h(q[0])
return squin.broadcast.measure(q)

assert _run_kernel(main, 3) == [ONE, ZERO, ZERO]


@pytest.mark.parametrize("lost", range(3))
def test_ccz_with_a_lost_operand_preserves_the_other_qubits(lost):
@squin.kernel
def main():
q = squin.qalloc(3)
squin.broadcast.h(q)
squin.qubit_loss(1.0, q[lost])
squin.ccz(q[0], q[1], q[2])
squin.broadcast.h(q)
return squin.broadcast.measure(q)

expected = [ZERO, ZERO, ZERO]
expected[lost] = LOST
assert _run_kernel(main, 3) == expected


def test_u3_and_phased_xz_gates():
@squin.kernel
def main():
Expand Down
8 changes: 4 additions & 4 deletions ppvm-python/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading