# How can I measure qutrits in the X basis using cirq?

I attempted to create a custom measurement class which, in my case, allows us to go from the z basis to x basis using a hadamard gate transformation, and then we measure wrt that new basis. However, this custom class is extremely slow compared to cirq.measure, does anyone know any better methods?

class CustomMeasurementGate(cirq.Gate):
"""
A class representing a measurement gate for qudits in Cirq.

Attributes:
dimension (int): The dimension of the qudit (e.g., 3 for qutrits).
key (str): The key used for storing measurement results.
basisGate (cirq.Gate, optional): A gate that maps the computational basis
to the desired measurement basis.

Methods:
_num_qubits_(): Returns the number of qubits (1 for a single qudit).
_qid_shape_(): Returns the shape of the qudit (a tuple with the
dimension).
_decompose_(qubits): Decomposes the gate into basis transformation and
measurement operations.
__str__(): Returns a string representation of the measurement gate.
_measurement_key_name_(): Returns the measurement key name.
"""

def __init__(self, dimension, key, basisGate=None):
"""
Initializes a CustomMeasurementGate.

Args:
dimension (int): The dimension of the qudit.
key (str): The key for storing measurement results.
basisGate (cirq.Gate, optional): A gate to transform the computational
basis to the desired measurement basis.
"""
self.dimension = dimension
self.key = key
self.basisGate = basisGate

def _num_qubits_(self):
"""
Returns the number of qubits this gate acts on.

Returns:
int: The number of qubits (always 1 for a single qudit).
"""
return 1

def _qid_shape_(self):
"""
Returns the shape of the qudit.

Returns:
tuple: A tuple containing the dimension of the qudit.
"""
return (self.dimension,)

def _decompose_(self, qubits):
"""
Decomposes the gate into operations.

Args:
qubits (Sequence[cirq.Qid]): The qubits this gate acts on.

Yields:
cirq.Operation: The operations that make up this gate.
"""
ops = []
if self.basisGate is not None:
ops.append(self.basisGate.on(*qubits))
ops.append(cirq.measure(*qubits, key=self.key))
return ops

def __str__(self):
"""
Returns a string representation of the measurement gate.

Returns:
str: A string representing the measurement gate.
"""
return f"M(d={self.dimension}, {self.key})"

def _measurement_key_name_(self):
"""
Returns the key name for measurement results.

Returns:
str: The key name for measurement results.
"""
return self.key
$$$$

• instead of defining a decomposition you can try to define the action directly by providing the gate's kraus representation by defining the _kraus_ method. if you do also add _has_kraus_ method that returns True Commented Jun 24 at 19:11
• so essentially just describing a singular matrix (in this instance the hadamard gate) but under the _kraus_` method? So how is this faster than decompose? Commented Jun 24 at 23:29