Skip to content

Commit

Permalink
Document Stabiliser
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-mills-cqc committed Nov 10, 2023
1 parent 9e410fb commit bbae32f
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 70 deletions.
10 changes: 6 additions & 4 deletions qermit/noise_model/noise_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ def counter_propagate(
for _ in range(n_counts):
stabiliser = self.random_propagate(cliff_circ, **kwargs)

if not stabiliser.is_identity():
if not stabiliser.is_identity:
error_counter.update([stabiliser])

return error_counter
Expand Down Expand Up @@ -516,7 +516,7 @@ def random_propagate(
f"Direction must be 'backward' or 'forward'. Is {direction}"
)

# For each command in the circuit, add an error ass appropriate, and
# For each command in the circuit, add an error as appropriate, and
# push the total error through the command.
for command in command_list:

Expand All @@ -543,10 +543,12 @@ def random_propagate(
for pauli, qubit in zip(error, command.args):
if direction == 'backward':
stabiliser.pre_apply_pauli(
pauli=pauli, qubit=qubit)
pauli=pauli, qubit=cast(Qubit, qubit)
)
elif direction == 'forward':
stabiliser.post_apply_pauli(
pauli=pauli, qubit=qubit)
pauli=pauli, qubit=cast(Qubit, qubit)
)
else:
raise Exception(
"Direction must be 'backward' or 'forward'. "
Expand Down
47 changes: 35 additions & 12 deletions qermit/noise_model/pauli_error_transpile.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
from pytket.pauli import Pauli # type: ignore
from pytket.passes import CustomPass # type: ignore
from pytket import Circuit, OpType
from pytket.pauli import Pauli
from pytket.passes import CustomPass, BasePass
from pytket import Circuit, OpType, Qubit
from .noise_model import NoiseModel
from typing import cast


def PauliErrorTranspile(noise_model):
def PauliErrorTranspile(noise_model: NoiseModel) -> BasePass:
"""Generates compiler pass which adds coherent noise to a circuit.
def add_gates(circuit):
:param noise_model: Model describing the noise to be added. Should be
a Pauli noise model.
:type noise_model: NoiseModel
:return: Compiler pass adding random coherent Pauli noise.
:rtype: BasePass
"""

def add_gates(circuit: Circuit) -> Circuit:
"""Function adding random coherent Pauli errors to a circuit.
:param circuit: Circuit to which errors are added.
:type circuit: Circuit
:raises Exception: Raised if the noise model is not a Pauli one.
:return: Circuit with additional noise operations.
:rtype: Circuit
"""

# Initialise circuit with the same registers as input.
noisy_circuit = Circuit()
for register in circuit.q_registers:
noisy_circuit.add_q_register(register)
for register in circuit.c_registers:
noisy_circuit.add_c_register(register)
for q_register in circuit.q_registers:
noisy_circuit.add_q_register(q_register)
for c_register in circuit.c_registers:
noisy_circuit.add_c_register(c_register)

# Add each command in the original circuit,
# and a pauli error if appropriate.
for command in circuit.get_commands():

if command.op.type == OpType.Barrier:
noisy_circuit.add_barrier(command.args)
else:
noisy_circuit.add_gate(command.op, command.args)

# If command has noise model defined, add a random error
if command.op.type in noise_model.noisy_gates:
# Sample a random error, which may be None
error = noise_model.get_error_distribution(
command.op.type
).sample()
Expand All @@ -30,11 +53,11 @@ def add_gates(circuit):
error
):
if pauli in [Pauli.X, OpType.X]:
noisy_circuit.X(qubit, opgroup='noisy')
noisy_circuit.X(cast(Qubit, qubit), opgroup='noisy')
elif pauli in [Pauli.Z, OpType.Z]:
noisy_circuit.Z(qubit, opgroup='noisy')
noisy_circuit.Z(cast(Qubit, qubit), opgroup='noisy')
elif pauli in [Pauli.Y, OpType.Y]:
noisy_circuit.Y(qubit, opgroup='noisy')
noisy_circuit.Y(cast(Qubit, qubit), opgroup='noisy')
elif pauli in [Pauli.I]:
pass
else:
Expand Down
Loading

0 comments on commit bbae32f

Please sign in to comment.