From b3c29a11d3c82285fb464e730f04c10d9107549b Mon Sep 17 00:00:00 2001 From: Dan Mills <52407433+daniel-mills-cqc@users.noreply.github.com> Date: Tue, 10 Dec 2024 15:35:31 +0000 Subject: [PATCH] Remove commute_coeff --- qermit/noise_model/noise_model.py | 18 ++++++++++----- qermit/noise_model/qermit_pauli.py | 30 +------------------------ tests/noise_model_test.py | 36 ------------------------------ 3 files changed, 14 insertions(+), 70 deletions(-) diff --git a/qermit/noise_model/noise_model.py b/qermit/noise_model/noise_model.py index 5c465706..fd182e5f 100644 --- a/qermit/noise_model/noise_model.py +++ b/qermit/noise_model/noise_model.py @@ -115,10 +115,14 @@ def to_ptm(self) -> Tuple[NDArray, Dict[Tuple[Pauli, ...], int]]: pauli_iterable=error, qubit_list=[Qubit(i) for i in range(self.n_qubits)], ) - - ptm[index][index] += error_rate * QermitPauli.commute_coeff( - pauli_one=pauli, pauli_two=error_pauli + commute_coeff = ( + 1 + if pauli.qubit_pauli_tensor.commutes_with( + error_pauli.qubit_pauli_tensor + ) + else -1 ) + ptm[index][index] += error_rate * commute_coeff # Some checks that the form of the PTM is correct. identity = tuple(Pauli.I for _ in range(self.n_qubits)) @@ -190,8 +194,12 @@ def from_ptm( pauli_iterable=pauli_two_tuple, qubit_list=[Qubit(i) for i in range(len(pauli_two_tuple))], ) - commutation_matrix[index_one][index_two] = QermitPauli.commute_coeff( - pauli_one=pauli_one, pauli_two=pauli_two + commutation_matrix[index_one][index_two] = ( + 1 + if pauli_one.qubit_pauli_tensor.commutes_with( + pauli_two.qubit_pauli_tensor + ) + else -1 ) error_rate_list = np.matmul(ptm.diagonal(), np.linalg.inv(commutation_matrix)) diff --git a/qermit/noise_model/qermit_pauli.py b/qermit/noise_model/qermit_pauli.py index a05fdd9d..98f9ec45 100644 --- a/qermit/noise_model/qermit_pauli.py +++ b/qermit/noise_model/qermit_pauli.py @@ -48,34 +48,6 @@ def __init__( self.phase = phase self.qubit_list = qubit_list - @staticmethod - def commute_coeff(pauli_one: QermitPauli, pauli_two: QermitPauli) -> int: - """Calculate the coefficient which result from commuting pauli_one - past pauli_two. That is to say P_2 P_1 = c P_1 P_2 where c is the - coefficient returned by this function. - - :param pauli_one: First Pauli - :param pauli_two: Second Pauli - :raises Exception: Raised if the Paulis do not act - on matching qubits. - :return: Coefficient resulting from commuting the two Paulis. - """ - if not pauli_one.qubit_list == pauli_two.qubit_list: - raise Exception( - "The given Paulis must act on the same qubits. " - + f"In this case the qubits acted on by pauli_one {pauli_one.qubit_list} " - + f"differ from those of pauli_two {pauli_two.qubit_list}." - ) - power = sum( - pauli_one.X_list[qubit] * pauli_two.Z_list[qubit] - for qubit in pauli_one.qubit_list - ) - power += sum( - pauli_one.Z_list[qubit] * pauli_two.X_list[qubit] - for qubit in pauli_one.qubit_list - ) - return (-1) ** power - def is_measureable(self, qubit_list: List[Qubit]) -> bool: """Checks if this Pauli would be measurable on the given qubits in the computational bases. That is to say if at least one Pauli on the given @@ -208,7 +180,7 @@ def __hash__(self): return hash(key) def __str__(self) -> str: - return str(self.qubit_pauli_tensor) + return str(self.qubit_pauli_tensor.string) + str(self.qubit_pauli_tensor.coeff) def __eq__(self, other: object) -> bool: """Checks for equality by checking all qubits match, and that all diff --git a/tests/noise_model_test.py b/tests/noise_model_test.py index 60fb942d..26a5a6d4 100644 --- a/tests/noise_model_test.py +++ b/tests/noise_model_test.py @@ -112,42 +112,6 @@ def test_qermit_pauli_from_iterable() -> None: pauli.qubit_pauli_tensor.coeff == 1 + 0j -def test_qermit_pauli_commute_coeff() -> None: - # This tests a few commutation coefficients - # which have been verified by hand. - verified_list = [ - # Single qubit Paulis - ((([0], [1]), ([1], [0])), -1), - ((([1], [1]), ([1], [1])), 1), - ((([1], [0]), ([0], [1])), -1), - ((([0], [0]), ([0], [1])), 1), - ((([1], [0]), ([0], [0])), 1), - ((([1], [1]), ([0], [1])), -1), - # Two qubit Paulis - ((([0, 1], [1, 0]), ([1, 0], [0, 1])), 1), - ((([0, 1], [1, 0]), ([0, 0], [1, 1])), -1), - ((([0, 0], [0, 0]), ([0, 0], [1, 1])), 1), - ] - - for verified in verified_list: - n_qubits = len(verified[0][0][0]) - - pauli_one = QermitPauli( - Z_list=verified[0][0][0], - X_list=verified[0][0][1], - qubit_list=[Qubit(i) for i in range(n_qubits)], - ) - pauli_two = QermitPauli( - Z_list=verified[0][1][0], - X_list=verified[0][1][1], - qubit_list=[Qubit(i) for i in range(n_qubits)], - ) - assert ( - QermitPauli.commute_coeff(pauli_one=pauli_one, pauli_two=pauli_two) - == verified[1] - ) - - def test_noise_model_logical_error_propagation() -> None: pytket_ciruit = Circuit(2).H(0).CX(0, 1).measure_all()