Skip to content

Commit

Permalink
Initialise with qubit pauli tensor
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-mills-cqc committed Dec 16, 2024
1 parent ec3aa13 commit 92d66d2
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 103 deletions.
12 changes: 6 additions & 6 deletions qermit/coherent_pauli_checks/pauli_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def add_pauli_checks_to_circbox(
circ=clifford_subcircuit,
)

print("start_stabiliser_list: ", start_stabiliser_list[0])

# TODO: check that register names do not already exist
control_qubit_list = [
Qubit(name="ancilla", index=i)
Expand Down Expand Up @@ -188,7 +190,7 @@ def sample(self, circ: Circuit) -> List[QermitPauli]:
:return: Z Pauli string of length equal to the circuit.
"""
return [
QermitPauli.from_qubit_pauli_tensor(
QermitPauli(
QubitPauliTensor(
string=QubitPauliString(
map={qubit: Pauli.Z for qubit in circ.qubits}
Expand All @@ -209,7 +211,7 @@ def sample(self, circ: Circuit) -> List[QermitPauli]:
:return: X Pauli string of length equal to the circuit.
"""
return [
QermitPauli.from_qubit_pauli_tensor(
QermitPauli(
QubitPauliTensor(
string=QubitPauliString(
map={qubit: Pauli.X for qubit in circ.qubits}
Expand Down Expand Up @@ -258,7 +260,7 @@ def sample(
coeff=1,
)
if qpt != QubitPauliTensor():
stabiliser_list.append(QermitPauli.from_qubit_pauli_tensor(qpt))
stabiliser_list.append(QermitPauli(qpt))

return stabiliser_list

Expand Down Expand Up @@ -352,8 +354,6 @@ def sample(
# )

return [
QermitPauli.from_qubit_pauli_tensor(
QubitPauliTensor(string=smallest_commute_prob_pauli, coeff=1)
)
QermitPauli(QubitPauliTensor(string=smallest_commute_prob_pauli, coeff=1))
for smallest_commute_prob_pauli in smallest_commute_prob_pauli_list
]
2 changes: 1 addition & 1 deletion qermit/noise_model/noise_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ def random_propagate(
:raises Exception: Raised if direction is invalid.
:return: Resulting logical error.
"""
pauli_error = QermitPauli.from_qubit_pauli_tensor(
pauli_error = QermitPauli(
QubitPauliTensor(
string=QubitPauliString(
map={qubit: Pauli.I for qubit in cliff_circ.qubits}
Expand Down
113 changes: 48 additions & 65 deletions qermit/noise_model/qermit_pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,22 @@ class QermitPauli:

coeff_to_phase = {1 + 0j: 0, 0 + 1j: 1, -1 + 0j: 2, 0 - 1j: 3}

def __init__(
self,
Z_list: list[int],
X_list: list[int],
qubit_list: list[Qubit],
phase: int = 0,
):
coeff = self.phase_dict[phase]

self.qubit_list = qubit_list

self.unitary_tableau = UnitaryTableau(nqb=len(qubit_list))

self.qubit_index = {qubit: Qubit(i) for i, qubit in enumerate(qubit_list)}
self.index_qubit = {Qubit(i): qubit for i, qubit in enumerate(qubit_list)}

paulis = []

for Z, X in zip(Z_list, X_list):
if X and Z:
paulis.append(Pauli.Y)
coeff *= -1j
elif X:
paulis.append(Pauli.X)
elif Z:
paulis.append(Pauli.Z)
else:
paulis.append(Pauli.I)
def __init__(self, qpt: QubitPauliTensor) -> None:
self.qubit_list = list(qpt.string.map.keys())

self.unitary_tableau = UnitaryTableau(nqb=len(self.qubit_list))

self.qubit_index = {qubit: Qubit(i) for i, qubit in enumerate(self.qubit_list)}
self.index_qubit = {Qubit(i): qubit for i, qubit in enumerate(self.qubit_list)}

self.input_pauli_tensor = QubitPauliTensor(
qubits=[qubit_index for qubit_index in self.qubit_index.values()],
paulis=paulis,
coeff=coeff,
string=QubitPauliString(
map={
self.qubit_index[qubit]: pauli
for qubit, pauli in qpt.string.map.items()
}
),
coeff=qpt.coeff,
)

def is_measureable(self, qubit_list: List[Qubit]) -> bool:
Expand Down Expand Up @@ -83,7 +66,7 @@ def reduce_qubits(self, qubit_list: List[Qubit]) -> QermitPauli:
:param qubit_list: Qubits in Pauli which should be removed.
:return: Reduced Pauli.
"""
return self.from_qubit_pauli_tensor(
return QermitPauli(
QubitPauliTensor(
string=QubitPauliString(
map={
Expand All @@ -101,52 +84,52 @@ def dagger(self) -> QermitPauli:
:return: Conjugate transpose of the Pauli.
"""
return QermitPauli.from_qubit_pauli_tensor(
return QermitPauli(
qpt=QubitPauliTensor(
string=self.qubit_pauli_tensor.string,
coeff=self.qubit_pauli_tensor.coeff.conjugate(),
)
)

@classmethod
def from_qubit_pauli_tensor(cls, qpt: QubitPauliTensor) -> QermitPauli:
"""Create a Pauli from a qubit pauli string.
# @classmethod
# def from_qubit_pauli_tensor(cls, qpt: QubitPauliTensor) -> QermitPauli:
# """Create a Pauli from a qubit pauli string.

:param qps: Qubit pauli string to be converted to a Pauli.
:return: Pauli created from qubit pauli string.
"""
# :param qps: Qubit pauli string to be converted to a Pauli.
# :return: Pauli created from qubit pauli string.
# """

Z_list = []
X_list = []
phase = cls.coeff_to_phase[qpt.coeff]
qubit_list = []
# Z_list = []
# X_list = []
# phase = cls.coeff_to_phase[qpt.coeff]
# qubit_list = []

qps = qpt.string
# qps = qpt.string

for pauli in qps.to_list():
qubit = Qubit(name=pauli[0][0], index=pauli[0][1])
qubit_list.append(qubit)
# for pauli in qps.to_list():
# qubit = Qubit(name=pauli[0][0], index=pauli[0][1])
# qubit_list.append(qubit)

if pauli[1] in ["Z", "Y"]:
Z_list.append(1)
else:
Z_list.append(0)
# if pauli[1] in ["Z", "Y"]:
# Z_list.append(1)
# else:
# Z_list.append(0)

if pauli[1] in ["X", "Y"]:
X_list.append(1)
else:
X_list.append(0)
# if pauli[1] in ["X", "Y"]:
# X_list.append(1)
# else:
# X_list.append(0)

if pauli[1] == "Y":
phase += 1
phase %= 4
# if pauli[1] == "Y":
# phase += 1
# phase %= 4

return cls(
Z_list=Z_list,
X_list=X_list,
qubit_list=qubit_list,
phase=phase,
)
# return cls(
# Z_list=Z_list,
# X_list=X_list,
# qubit_list=qubit_list,
# phase=phase,
# )

def __hash__(self):
return self.qubit_pauli_tensor.__hash__()
Expand Down Expand Up @@ -354,7 +337,7 @@ def from_pauli_list(
:param qubit_list: The qubits on which the resulting pauli will act.
:return: The pauli corresponding to the given iterable.
"""
return cls.from_qubit_pauli_tensor(
return cls(
qpt=QubitPauliTensor(
string=QubitPauliString(
qubits=qubit_list,
Expand Down
12 changes: 6 additions & 6 deletions tests/coherent_pauli_checks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ def test_optimal_pauli_sampler():
circ=cliff_circ,
)

assert stab[0] == QermitPauli.from_qubit_pauli_tensor(
assert stab[0] == QermitPauli(
QubitPauliTensor(
string=QubitPauliString(qubits=qubits, paulis=[Pauli.I, Pauli.I, Pauli.Y]),
coeff=1,
Expand Down Expand Up @@ -836,7 +836,7 @@ class DeterministicPauliSampler(PauliSampler):
def sample(self, circ, **kwargs):
qubit_list = circ.qubits
return [
QermitPauli.from_qubit_pauli_tensor(
QermitPauli(
QubitPauliTensor(
string=QubitPauliString(
pauli=Pauli.Y,
Expand Down Expand Up @@ -974,7 +974,7 @@ def test_error_sampler():
)
ideal = Counter(
{
QermitPauli.from_qubit_pauli_tensor(
QermitPauli(
QubitPauliTensor(
string=QubitPauliString(
map={
Expand All @@ -986,7 +986,7 @@ def test_error_sampler():
coeff=1,
)
): 190,
QermitPauli.from_qubit_pauli_tensor(
QermitPauli(
QubitPauliTensor(
string=QubitPauliString(
map={
Expand All @@ -998,11 +998,11 @@ def test_error_sampler():
coeff=1,
)
): 164,
QermitPauli.from_qubit_pauli_tensor(
QermitPauli(
QubitPauliTensor(
string=QubitPauliString(
map={
Qubit(name="ancilla", index=0): Pauli.X,
Qubit(name="ancilla", index=0): Pauli.I,
Qubit(name="my_reg", index=0): Pauli.X,
Qubit(name="my_reg", index=1): Pauli.X,
}
Expand Down
Loading

0 comments on commit 92d66d2

Please sign in to comment.