From 6867816a350b574aab2690522f4428ca559376e3 Mon Sep 17 00:00:00 2001 From: Dan Mills <52407433+daniel-mills-cqc@users.noreply.github.com> Date: Tue, 10 Dec 2024 12:14:27 +0000 Subject: [PATCH] Replace qubit_pauli_string with qubit_pauli_tensor (#200) --- qermit/noise_model/noise_model.py | 2 +- qermit/noise_model/qermit_pauli.py | 31 ++++--------- tests/coherent_pauli_checks_test.py | 9 ++-- tests/noise_model_test.py | 72 +++++++++++++---------------- 4 files changed, 47 insertions(+), 67 deletions(-) diff --git a/qermit/noise_model/noise_model.py b/qermit/noise_model/noise_model.py index f4baf982..5c465706 100644 --- a/qermit/noise_model/noise_model.py +++ b/qermit/noise_model/noise_model.py @@ -409,7 +409,7 @@ def distribution(self) -> Dict[QubitPauliString, float]: distribution: Dict[QubitPauliString, float] = {} for stab, count in dict(self.pauli_error_counter).items(): # Note that the phase is ignored here - pauli_string, _ = stab.qubit_pauli_string + pauli_string = stab.qubit_pauli_tensor.string distribution[pauli_string] = ( distribution.get(pauli_string, 0) + count / self.total ) diff --git a/qermit/noise_model/qermit_pauli.py b/qermit/noise_model/qermit_pauli.py index ac8d8a11..95b827bf 100644 --- a/qermit/noise_model/qermit_pauli.py +++ b/qermit/noise_model/qermit_pauli.py @@ -7,7 +7,7 @@ import numpy as np from numpy.random import Generator from pytket.circuit import Circuit, OpType, Qubit -from pytket.pauli import Pauli, QubitPauliString +from pytket.pauli import Pauli, QubitPauliString, QubitPauliTensor class QermitPauli: @@ -208,8 +208,7 @@ def __hash__(self): return hash(key) def __str__(self) -> str: - qubit_pauli_string, operator_phase = self.qubit_pauli_string - return f"{qubit_pauli_string}, {operator_phase}" + return str(self.qubit_pauli_tensor) def __eq__(self, other: object) -> bool: """Checks for equality by checking all qubits match, and that all @@ -538,10 +537,10 @@ def circuit(self) -> Circuit: return circ @property - def pauli_string(self) -> Tuple[List[Pauli], complex]: - """List of Paulis which correspond to Pauli, and the phase. + def qubit_pauli_tensor(self) -> QubitPauliTensor: + """Return QubitPauliTensor describing Pauli. - :return: [description] + :return: QubitPauliTensor describing Pauli. """ operator_phase = self.phase @@ -558,21 +557,11 @@ def pauli_string(self) -> Tuple[List[Pauli], complex]: operator_phase += 3 operator_phase %= 4 - return paulis, self.phase_dict[operator_phase] - - @property - def qubit_pauli_string(self) -> Tuple[QubitPauliString, complex]: - """Qubit pauli string corresponding to Pauli, - along with the appropriate phase. - - :return: Pauli string and phase corresponding to Pauli. - """ - - paulis, operator_phase = self.pauli_string - - qubit_pauli_string = QubitPauliString(qubits=self.qubit_list, paulis=paulis) - - return qubit_pauli_string, operator_phase + return QubitPauliTensor( + qubits=self.qubit_list, + paulis=paulis, + coeff=self.phase_dict[operator_phase], + ) @classmethod def from_pauli_iterable( diff --git a/tests/coherent_pauli_checks_test.py b/tests/coherent_pauli_checks_test.py index b2cc1777..028f1731 100644 --- a/tests/coherent_pauli_checks_test.py +++ b/tests/coherent_pauli_checks_test.py @@ -336,12 +336,11 @@ def test_logical_error_coherent_pauli_check_workflow(): ) pauli_sampler = OptimalPauliSampler(noise_model=noise_model, n_checks=1) - assert pauli_sampler.sample(circ=cliff_sub_circ)[0].qubit_pauli_string == ( - QubitPauliString( - qubits=[Qubit(0), Qubit(1), Qubit(2)], paulis=[Pauli.I, Pauli.I, Pauli.Z] - ), - 1, + pauli = pauli_sampler.sample(circ=cliff_sub_circ)[0] + assert pauli.qubit_pauli_tensor.string == QubitPauliString( + qubits=[Qubit(0), Qubit(1), Qubit(2)], paulis=[Pauli.I, Pauli.I, Pauli.Z] ) + assert pauli.qubit_pauli_tensor.coeff == 1 cliff_circ = Circuit() cliff_circ.add_q_register(name="my_reg", size=3) diff --git a/tests/noise_model_test.py b/tests/noise_model_test.py index baf66689..b719f252 100644 --- a/tests/noise_model_test.py +++ b/tests/noise_model_test.py @@ -108,7 +108,8 @@ def test_qermit_pauli_from_iterable() -> None: pauli_iterable=qubit_pauli_string.map.values(), qubit_list=list(qubit_pauli_string.map.keys()), ) - pauli.qubit_pauli_string == (qubit_pauli_string, 1 + 0j) + pauli.qubit_pauli_tensor.string == qubit_pauli_string + pauli.qubit_pauli_tensor.coeff == 1 + 0j def test_qermit_pauli_commute_coeff() -> None: @@ -606,11 +607,10 @@ def test_identity_clifford() -> None: ) pauli.apply_circuit(circ) - qubit_pauli_string, phase = pauli.qubit_pauli_string - assert qubit_pauli_string == QubitPauliString( + assert pauli.qubit_pauli_tensor.string == QubitPauliString( qubits=qubit_list, paulis=[Pauli.Z, Pauli.Z] ) - assert phase == 1 + assert pauli.qubit_pauli_tensor.coeff == 1 def test_H() -> None: @@ -618,14 +618,16 @@ def test_H() -> None: pauli = QermitPauli(Z_list=[1], X_list=[0], qubit_list=qubit_list) pauli.H(qubit=qubit_list[0]) - qubit_pauli_string, phase = pauli.qubit_pauli_string - assert qubit_pauli_string == QubitPauliString(qubits=qubit_list, paulis=[Pauli.X]) - assert phase == 1 + assert pauli.qubit_pauli_tensor.string == QubitPauliString( + qubits=qubit_list, paulis=[Pauli.X] + ) + assert pauli.qubit_pauli_tensor.coeff == 1 pauli.H(qubit=qubit_list[0]) - qubit_pauli_string, phase = pauli.qubit_pauli_string - assert qubit_pauli_string == QubitPauliString(qubits=qubit_list, paulis=[Pauli.Z]) - assert phase == 1 + assert pauli.qubit_pauli_tensor.string == QubitPauliString( + qubits=qubit_list, paulis=[Pauli.Z] + ) + assert pauli.qubit_pauli_tensor.coeff == 1 def test_h_series_gates() -> None: @@ -643,11 +645,10 @@ def test_apply_circuit() -> None: pauli.apply_circuit(circ) - qubit_pauli_string, phase = pauli.qubit_pauli_string - assert qubit_pauli_string == QubitPauliString( + assert pauli.qubit_pauli_tensor.string == QubitPauliString( qubits=qubit_list, paulis=[Pauli.X, Pauli.Y] ) - assert phase == 1 + assert pauli.qubit_pauli_tensor.coeff == 1 def test_apply_gate() -> None: @@ -660,11 +661,10 @@ def test_apply_gate() -> None: pauli.apply_gate(op_type=OpType.H, qubits=[qubit_list[0]]) - qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string - assert qermit_qubit_pauli_string == QubitPauliString( + assert pauli.qubit_pauli_tensor.string == QubitPauliString( qubits=qubit_list, paulis=[Pauli.X, Pauli.Z, Pauli.Z] ) - assert phase == 1 + assert pauli.qubit_pauli_tensor.coeff == 1 pauli.apply_gate( op_type=OpType.CX, @@ -674,15 +674,13 @@ def test_apply_gate() -> None: qubit_pauli_string = QubitPauliString( qubits=qubit_list, paulis=[Pauli.X, Pauli.I, Pauli.Z] ) - qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string - assert qermit_qubit_pauli_string == qubit_pauli_string - assert phase == 1 + assert pauli.qubit_pauli_tensor.string == qubit_pauli_string + assert pauli.qubit_pauli_tensor.coeff == 1 pauli.apply_gate(op_type=OpType.S, qubits=[qubit_list[1]]) - qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string - assert qermit_qubit_pauli_string == qubit_pauli_string - assert phase == 1 + assert pauli.qubit_pauli_tensor.string == qubit_pauli_string + assert pauli.qubit_pauli_tensor.coeff == 1 def test_qubit_pauli_string() -> None: @@ -696,9 +694,8 @@ def test_qubit_pauli_string() -> None: qubit_pauli_string = QubitPauliString( qubits=qubit_list, paulis=[Pauli.Z, Pauli.Z, Pauli.Z] ) - qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string - assert qermit_qubit_pauli_string == qubit_pauli_string - assert phase == 1 + assert pauli.qubit_pauli_tensor.string == qubit_pauli_string + assert pauli.qubit_pauli_tensor.coeff == 1 pauli.H(qubit_list[0]) pauli.S(qubit_list[0]) @@ -706,27 +703,24 @@ def test_qubit_pauli_string() -> None: qubit_pauli_string = QubitPauliString( qubits=qubit_list, paulis=[Pauli.Y, Pauli.Z, Pauli.Z] ) - qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string - assert qermit_qubit_pauli_string == qubit_pauli_string - assert phase == 1 + assert pauli.qubit_pauli_tensor.string == qubit_pauli_string + assert pauli.qubit_pauli_tensor.coeff == 1 pauli.CX(qubit_list[0], qubit_list[1]) qubit_pauli_string = QubitPauliString( qubits=qubit_list, paulis=[Pauli.X, Pauli.Y, Pauli.Z] ) - qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string - assert qermit_qubit_pauli_string == qubit_pauli_string - assert phase == 1 + assert pauli.qubit_pauli_tensor.string == qubit_pauli_string + assert pauli.qubit_pauli_tensor.coeff == 1 pauli.S(qubit_list[1]) qubit_pauli_string = QubitPauliString( qubits=qubit_list, paulis=[Pauli.X, Pauli.X, Pauli.Z] ) - qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string - assert qermit_qubit_pauli_string == qubit_pauli_string - assert phase == -1 + assert pauli.qubit_pauli_tensor.string == qubit_pauli_string + assert pauli.qubit_pauli_tensor.coeff == -1 pauli.S(qubit_list[0]) pauli.CX(qubit_list[0], qubit_list[2]) @@ -734,9 +728,8 @@ def test_qubit_pauli_string() -> None: qubit_pauli_string = QubitPauliString( qubits=qubit_list, paulis=[Pauli.X, Pauli.X, Pauli.Y] ) - qermit_qubit_pauli_string, phase = pauli.qubit_pauli_string - assert qermit_qubit_pauli_string == qubit_pauli_string - assert phase == -1 + assert pauli.qubit_pauli_tensor.string == qubit_pauli_string + assert pauli.qubit_pauli_tensor.coeff == -1 def test_clifford_incremental() -> None: @@ -801,9 +794,8 @@ def test_to_from_qps() -> None: paulis=paulis, ) stab = QermitPauli.from_qubit_pauli_string(qubit_pauli_string) - stab_qps, stab_phase = stab.qubit_pauli_string - assert stab_qps == qubit_pauli_string - assert stab_phase == 1 + 0j + assert stab.qubit_pauli_tensor.string == qubit_pauli_string + assert stab.qubit_pauli_tensor.coeff == 1 + 0j def test_is_measureable() -> None: