Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 19 additions & 4 deletions preview/logical/python/cudaq/logical/ops/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,25 @@ def move(values, *, via, trajectory=None):


def measure(value, *, basis="z", destructive=True, record=None):
return require_trace("measure").measure(value,
basis=basis,
destructive=destructive,
record=record)
from cudaq.logical.programs.builder import UnplacedBuilder

trace = require_trace("measure")
if isinstance(trace, UnplacedBuilder):
# A logical program measures destructively in Z or X, as measure_z and
# measure_x do.
if not destructive or record is not None:
raise TypeError(
"cudaq.logical.measure(destructive=False) and record= are "
"available only in a P3 physical trace")
Comment thread
burrows99 marked this conversation as resolved.
Resolved
basis = str(basis).lower()
if basis not in ("x", "z"):
raise ValueError(
f"logical measurement basis must be 'x' or 'z', got {basis!r}")
Comment thread
burrows99 marked this conversation as resolved.
Resolved
return trace.measure(basis, value)
return trace.measure(value,
basis=basis,
destructive=destructive,
record=record)


def delay(values, *, duration_ns):
Expand Down
38 changes: 38 additions & 0 deletions preview/logical/python/tests/cudaq/logical/test_cudaq_logical.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,44 @@ def noisy():
assert "the target does not support noise" in capfd.readouterr().err


@pytest.mark.parametrize("basis", ["z", "x", "Z", "X"])
def test_generic_measure_in_a_logical_program(basis):
import cudaq.logical as cql

@cql.program
def generic() -> tuple[bool]:
q = cql.allocate(1, state=cql.types.zero)
return (cql.measure(q[0], basis=basis),)

text = cql.compile(generic).to_mlir()
assert text.count(f"qlx.measure <{basis.upper()}>") == 1
assert text.count("qlx.measure") == 1


@pytest.mark.parametrize("options, error, match", [
({
"destructive": False
}, TypeError, "only in a P3 physical trace"),
({
"record": "m0"
}, TypeError, "only in a P3 physical trace"),
({
"basis": "y"
}, ValueError, "basis must be 'x' or 'z'"),
])
def test_generic_measure_in_a_logical_program_rejects_physical_options(
options, error, match):
import cudaq.logical as cql

@cql.program
def generic() -> tuple[bool]:
q = cql.allocate(1, state=cql.types.zero)
return (cql.measure(q[0], **options),)

with pytest.raises(error, match=match):
cql.compile(generic)


def test_cudaq_qvector_and_scalar_import_as_zero_preparation():
import cudaq
import cudaq.logical as cql
Expand Down
Loading