tensorcircuit.zx.converter¶

Converter from TensorCircuit to PyZX. Includes graph preparation and reduction utilities.

class tensorcircuit.zx.converter.GraphRepresentation(graph: 'GraphS' = <factory>, rec: 'List[int]' = <factory>, silent_rec: 'List[int]' = <factory>, detectors: 'List[int]' = <factory>, observables_dict: 'Dict[int, int]'=<factory>, first_vertex: 'Dict[int, int]'=<factory>, last_vertex: 'Dict[int, int]'=<factory>, channel_probs: 'List[Any]' = <factory>, correlated_error_probs: 'List[float]' = <factory>, num_error_bits: 'int' = 0, num_correlated_error_bits: 'int' = 0)[source]¶

Bases: object

__init__(graph: GraphS = <factory>, rec: List[int] = <factory>, silent_rec: List[int] = <factory>, detectors: List[int] = <factory>, observables_dict: Dict[int, int]=<factory>, first_vertex: Dict[int, int]=<factory>, last_vertex: Dict[int, int]=<factory>, channel_probs: List[Any] = <factory>, correlated_error_probs: List[float] = <factory>, num_error_bits: int = 0, num_correlated_error_bits: int = 0) None¶
add_edge(e: Any, t: Any = EdgeType.SIMPLE) None[source]¶
add_edge_table(etab: dict[tuple[Any, Any], list[int]]) None[source]¶
add_edges(edges: list[tuple[Any, Any]], t: Any = EdgeType.SIMPLE) None[source]¶
add_to_phase(v: Any, p: Any, params: Any = None) None[source]¶
add_vertex(t: Any = VertexType.Z, qubit: int = -1, row: float = -1, phase: Any = 0) int[source]¶

Add a vertex to the graph.

Parameters:
  • t (Any, optional) – Vertex type, defaults to VertexType.Z.

  • qubit (int, optional) – Qubit index, defaults to -1.

  • row (float, optional) – Row index for layout, defaults to -1.

  • phase (Any, optional) – Vertex phase, defaults to 0.

Returns:

The index of the new vertex.

Return type:

int

channel_probs: List[Any]¶
copy() GraphRepresentation[source]¶
correlated_error_probs: List[float]¶
detectors: List[int]¶
edge(v1: Any, v2: Any) Any[source]¶
edge_set() Any[source]¶
edge_st(e: Any) Any[source]¶

Get the endpoints of an edge.

Parameters:

e (Any) – Edge.

Returns:

Tuple of vertex indices (v1, v2).

Return type:

Any

edge_type(e: Any) Any[source]¶
edges(*args: Any) Any[source]¶
first_vertex: Dict[int, int]¶
fuse_phases(v1: Any, v2: Any) None[source]¶
get_auto_simplify() bool[source]¶
get_params(v: Any) Any[source]¶
graph: GraphS¶
incident_edges(v: Any) Any[source]¶

Get edges incident to a vertex.

Parameters:

v (Any) – Vertex index.

Returns:

Iterable of edges.

Return type:

Any

inputs() Any[source]¶
is_ground(v: Any) bool[source]¶
is_multigraph() bool[source]¶
last_vertex: Dict[int, int]¶
property merge_vdata: Any¶
neighbors(v: Any) Any[source]¶
num_correlated_error_bits: int = 0¶
num_edges() int[source]¶
num_error_bits: int = 0¶
num_vertices() int[source]¶
property observables: list[int]¶

List of observable vertices.

Returns:

List of vertex indices.

Return type:

list[int]

observables_dict: Dict[int, int]¶
outputs() Any[source]¶
phase(v: Any) Any[source]¶
phases() Any[source]¶
qubit(v: Any) Any[source]¶
qubits() Any[source]¶
rec: List[int]¶
remove_edge(e: Any) None[source]¶
remove_edges(edges: list[tuple[Any, Any]]) None[source]¶
remove_isolated_vertices() None[source]¶
remove_vertex(v: Any) None[source]¶
remove_vertices(vertices: list[Any]) None[source]¶
row(v: Any) Any[source]¶
rows() Any[source]¶
property scalar: Any¶
set_auto_simplify(v: bool) None[source]¶
set_edge_type(e: Any, t: Any) None[source]¶
set_ground(v: Any, g: bool = True) None[source]¶
set_inputs(v: Any) None[source]¶
set_outputs(v: Any) None[source]¶
set_phase(v: Any, p: Any) None[source]¶
set_qubit(v: Any, q: Any) None[source]¶
set_row(v: Any, r: Any) None[source]¶
set_type(v: Any, t: Any) None[source]¶
set_vdata(v: Any, key: str, val: Any) None[source]¶
silent_rec: List[int]¶
to_tensor() Any[source]¶
property track_phases: bool¶
type(v: Any) Any[source]¶
types() Any[source]¶
update_phase_index(v1: Any, v2: Any) None[source]¶
vdata(v: Any, key: str) Any[source]¶
vdata_keys(v: Any) Any[source]¶
vertex_degree(v: Any) int[source]¶
vertex_set() Any[source]¶
vertices() Any[source]¶
class tensorcircuit.zx.converter.SamplingGraph(graph: 'Any', error_transform: 'Any', channel_probs: 'list[Any]', num_outputs: 'int', num_detectors: 'int', num_error_bits: 'int', observables: 'list[int]' = <factory>)[source]¶

Bases: object

__init__(graph: ~typing.Any, error_transform: ~typing.Any, channel_probs: list[~typing.Any], num_outputs: int, num_detectors: int, num_error_bits: int, observables: list[int] = <factory>) None¶
channel_probs: list[Any]¶
error_transform: Any¶
graph: Any¶
num_detectors: int¶
num_error_bits: int¶
num_outputs: int¶
observables: list[int]¶
tensorcircuit.zx.converter.add_dummy(b: GraphRepresentation, qubit: int, row: float | int | None = None) int[source]¶

Add a dummy boundary vertex to a qubit lane.

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • qubit (int) – The qubit index.

  • row (float | int | None, optional) – Row index, defaults to last_row + 1.

Returns:

The index of the new vertex.

Return type:

int

tensorcircuit.zx.converter.add_lane(b: GraphRepresentation, qubit: int) int[source]¶

Initialize a new qubit lane with two boundary vertices.

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • qubit (int) – The qubit index.

Returns:

The index of the first vertex.

Return type:

int

tensorcircuit.zx.converter.build_amplitude_graph(built: GraphRepresentation, bitstring: Sequence[int]) GraphS[source]¶

Build a scalar graph representing the amplitude <bitstring|psi>.

Parameters:
  • built (GraphRepresentation) – The input ZX representation.

  • bitstring (Sequence[int]) – The target bitstring as a sequence of 0s and 1s.

Returns:

The scalar ZX graph.

Return type:

GraphS

tensorcircuit.zx.converter.build_sampling_graph(built: GraphRepresentation, sample_detectors: bool, pauli: Dict[int, str] | None = None) GraphS[source]¶

Build a doubled sampling graph from a ZX representation.

Parameters:
  • built (GraphRepresentation) – The input ZX representation.

  • sample_detectors (bool) – Whether to prepare for detector/observable sampling or measurement records.

  • pauli (Optional[Dict[int, str]]) – Optional Pauli string to insert at the junction for expectation values. Dictionary mapping qubit index to Pauli operator (‘I’, ‘X’, ‘Y’, ‘Z’).

Returns:

The prepared ZX graph.

Return type:

GraphS

tensorcircuit.zx.converter.circuit_to_zx(c: AbstractCircuit, force_measure_all: bool = False) GraphRepresentation[source]¶

Convert a TensorCircuit AbstractCircuit to a ZX-calculus GraphRepresentation.

Parameters:
  • c (AbstractCircuit) – The source circuit.

  • force_measure_all (bool, optional) – Whether to force measurements on all qubits at the end, defaults to False.

Returns:

The ZX graph representation of the circuit.

Return type:

GraphRepresentation

tensorcircuit.zx.converter.depolarize1(b: GraphRepresentation, qubit: int, p: float) None[source]¶

Apply single-qubit depolarizing channel.

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • qubit (int) – Qubit index.

  • p (float) – Depolarizing probability.

tensorcircuit.zx.converter.depolarize2(b: GraphRepresentation, q1: int, q2: int, p: float) None[source]¶

Apply two-qubit depolarizing channel.

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • q1 (int) – First qubit index.

  • q2 (int) – Second qubit index.

  • p (float) – Depolarizing probability.

tensorcircuit.zx.converter.detector(b: GraphRepresentation, rec: list[int]) None[source]¶

Add a detector vertex defined by a set of measurement outcomes.

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • rec (list[int]) – Indices of measurement records involved in the detector.

tensorcircuit.zx.converter.ensure_lane(b: GraphRepresentation, qubit: int) None[source]¶

Ensure a qubit lane exists in the graph.

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • qubit (int) – The qubit index.

tensorcircuit.zx.converter.h_gate(b: GraphRepresentation, qubit: int) None[source]¶

Apply Hadamard gate.

tensorcircuit.zx.converter.h_xy(b: GraphRepresentation, qubit: int) None[source]¶

Apply variant of Hadamard gate that swaps the X and Y axes (instead of X and Z).

tensorcircuit.zx.converter.h_yz(b: GraphRepresentation, qubit: int) None[source]¶

Apply variant of Hadamard gate that swaps the Y and Z axes (instead of X and Z).

tensorcircuit.zx.converter.is_pauli(matrix: Any) str | None[source]¶

Check if a matrix is a Pauli matrix.

Parameters:

matrix (np.ndarray) – The matrix to check.

Returns:

The name of the Pauli matrix (‘i’, ‘x’, ‘y’, ‘z’) or None.

Return type:

Optional[str]

tensorcircuit.zx.converter.last_edge(b: GraphRepresentation, qubit: int) Any[source]¶

Get the last edge on a qubit lane.

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • qubit (int) – The qubit index.

Returns:

The last edge index.

Return type:

Any

tensorcircuit.zx.converter.last_row(b: GraphRepresentation, qubit: int) float[source]¶

Get the row index of the last vertex on a qubit lane.

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • qubit (int) – The qubit index.

Returns:

Highest row index on the lane.

Return type:

float

tensorcircuit.zx.converter.m(b: GraphRepresentation, qubit: int, p: float = 0, invert: bool = False) None[source]¶

Measure qubit in Z basis with optional bit-flip error probability p.

tensorcircuit.zx.converter.mpp(b: GraphRepresentation, paulis: list[tuple[str, int]], invert: bool = False) None[source]¶

Measure a multi-Pauli product.

Identical implementation to tsim.core.instructions.mpp.

Parameters:
  • b – The graph representation to modify.

  • paulis – List of (pauli_type, qubit) pairs defining the Pauli product. pauli_type should be ‘X’, ‘Y’, or ‘Z’.

  • invert – Whether to invert the measurement result.

tensorcircuit.zx.converter.mr(b: GraphRepresentation, qubit: int, p: float = 0, invert: bool = False) None[source]¶

Z-basis demolition measurement (optionally noisy).

tensorcircuit.zx.converter.mrx(b: GraphRepresentation, qubit: int, p: float = 0, invert: bool = False) None[source]¶

X-basis demolition measurement (optionally noisy).

tensorcircuit.zx.converter.mry(b: GraphRepresentation, qubit: int, p: float = 0, invert: bool = False) None[source]¶

Y-basis demolition measurement (optionally noisy).

tensorcircuit.zx.converter.mrz(b: GraphRepresentation, qubit: int, p: float = 0, invert: bool = False) None[source]¶

Z-basis demolition measurement (optionally noisy).

tensorcircuit.zx.converter.mx(b: GraphRepresentation, qubit: int, p: float = 0, invert: bool = False) None[source]¶

Apply X-basis measurement.

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • qubit (int) – Qubit index.

  • p (float, optional) – Error probability, defaults to 0.

  • invert (bool, optional) – Whether to invert measurement result, defaults to False.

tensorcircuit.zx.converter.my(b: GraphRepresentation, qubit: int, p: float = 0, invert: bool = False) None[source]¶

Apply Y-basis measurement.

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • qubit (int) – Qubit index.

  • p (float, optional) – Error probability, defaults to 0.

  • invert (bool, optional) – Whether to invert measurement result, defaults to False.

tensorcircuit.zx.converter.observable_include(b: GraphRepresentation, rec: list[int], idx: int) None[source]¶

Add an observable vertex defined by a set of measurement outcomes.

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • rec (list[int]) – Indices of measurement records involved in the observable.

  • idx (int) – Index of the observable.

tensorcircuit.zx.converter.pauli_channel_1(b: GraphRepresentation, qubit: int, px: float = 0, py: float = 0, pz: float = 0) None[source]¶

Apply single-qubit Pauli channel.

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • qubit (int) – Qubit index.

  • px (float, optional) – Probability of X error, defaults to 0.

  • py (float, optional) – Probability of Y error, defaults to 0.

  • pz (float, optional) – Probability of Z error, defaults to 0.

tensorcircuit.zx.converter.pauli_channel_2(b: GraphRepresentation, qubit_i: int, qubit_j: int, pix: float = 0, piy: float = 0, piz: float = 0, pxi: float = 0, pxx: float = 0, pxy: float = 0, pxz: float = 0, pyi: float = 0, pyx: float = 0, pyy: float = 0, pyz: float = 0, pzi: float = 0, pzx: float = 0, pzy: float = 0, pzz: float = 0) None[source]¶

Apply two-qubit Pauli channel.

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • qubit_i (int) – First qubit index.

  • qubit_j (int) – Second qubit index.

  • pix – Probability of error IX.

…

tensorcircuit.zx.converter.prepare_graph(circuit: AbstractCircuit, *, sample_detectors: bool, force_measure_all: bool = False, pauli: Dict[int, str] | None = None, reset_scalar: bool = True) SamplingGraph[source]¶

Prepare a circuit for sampling by converting to ZX and reducing.

Parameters:
  • circuit (AbstractCircuit) – The input circuit.

  • sample_detectors (bool) – Whether to prepare for detector/observable sampling.

  • force_measure_all (bool, optional) – Whether to force final measurements, defaults to False.

  • pauli (Optional[Dict[int, str]]) – Optional Pauli string to insert at the junction.

Returns:

A SamplingGraph object containing the reduced graph and metadata.

Return type:

SamplingGraph

tensorcircuit.zx.converter.r_x(b: GraphRepresentation, qubit: int, phase: Fraction) None[source]¶

Apply R_X rotation gate with given phase (in units of π).

tensorcircuit.zx.converter.r_y(b: GraphRepresentation, qubit: int, phase: Fraction) None[source]¶

Apply R_Y rotation gate with given phase (in units of π).

tensorcircuit.zx.converter.r_z(b: GraphRepresentation, qubit: int, phase: Fraction) None[source]¶

Apply R_Z rotation gate with given phase (in units of π).

tensorcircuit.zx.converter.reset_x(b: GraphRepresentation, qubit: int) None[source]¶

Reset qubit in X basis.

Parameters:
tensorcircuit.zx.converter.reset_y(b: GraphRepresentation, qubit: int) None[source]¶

Reset qubit in Y basis.

Parameters:
tensorcircuit.zx.converter.reset_z(b: GraphRepresentation, qubit: int, p: float = 0) None[source]¶

Reset qubit to |0> state (Z-basis reset).

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • qubit (int) – Qubit index.

  • p (float, optional) – Error probability, defaults to 0.

tensorcircuit.zx.converter.sqrt_x(b: GraphRepresentation, qubit: int) None[source]¶

Apply SQRT_X gate.

tensorcircuit.zx.converter.sqrt_x_dag(b: GraphRepresentation, qubit: int) None[source]¶

Apply SQRT_X_DAG gate.

tensorcircuit.zx.converter.sqrt_y(b: GraphRepresentation, qubit: int) None[source]¶

Apply SQRT_Y gate.

tensorcircuit.zx.converter.sqrt_y_dag(b: GraphRepresentation, qubit: int) None[source]¶

Apply SQRT_Y_DAG gate.

tensorcircuit.zx.converter.sqrt_z(b: GraphRepresentation, qubit: int) None[source]¶

Apply SQRT_Z gate.

tensorcircuit.zx.converter.sqrt_z_dag(b: GraphRepresentation, qubit: int) None[source]¶

Apply SQRT_Z_DAG gate.

tensorcircuit.zx.converter.squash_graph(g: Any) None[source]¶

Compact the graph layout by re-assigning qubit and row indices.

Parameters:

g (Any) – The ZX graph.

tensorcircuit.zx.converter.transform_error_basis(g: Any, num_e: int | None = None) tuple[Any, Any][source]¶

Transform error bit variables from original ‘e’ basis to a reduced ‘f’ basis.

Parameters:
  • g (Any) – The ZX graph containing error variables.

  • num_e (int, optional) – Total number of error bits, defaults to None.

Returns:

A tuple of (transformed_graph, basis_transformation_matrix).

Return type:

tuple[Any, Any]

tensorcircuit.zx.converter.u3(b: GraphRepresentation, qubit: int, theta: Fraction, phi: Fraction, lambda_: Fraction) None[source]¶

Apply U3 gate: U3(θ,φ,λ) = R_Z(φ)·R_Y(θ)·R_Z(λ).

tensorcircuit.zx.converter.x_error(b: GraphRepresentation, qubit: int, p: float) None[source]¶
tensorcircuit.zx.converter.x_gate(b: GraphRepresentation, qubit: int) None[source]¶

Apply Pauli X gate.

tensorcircuit.zx.converter.x_phase(b: GraphRepresentation, qubit: int, phase: Fraction) None[source]¶

Apply an X spider with the given phase to a qubit.

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • qubit (int) – The qubit index.

  • phase (Fraction) – Phase in units of Ï€.

tensorcircuit.zx.converter.y_error(b: GraphRepresentation, qubit: int, p: float) None[source]¶

Apply Y error with probability p.

tensorcircuit.zx.converter.y_gate(b: GraphRepresentation, qubit: int) None[source]¶

Apply Pauli Y gate.

tensorcircuit.zx.converter.y_phase(b: GraphRepresentation, qubit: int, phase: Fraction) None[source]¶

Apply Y-basis spider with the given phase.

tensorcircuit.zx.converter.z_error(b: GraphRepresentation, qubit: int, p: float) None[source]¶
tensorcircuit.zx.converter.z_gate(b: GraphRepresentation, qubit: int) None[source]¶

Apply Pauli Z gate.

tensorcircuit.zx.converter.z_phase(b: GraphRepresentation, qubit: int, phase: Fraction) None[source]¶

Apply a Z spider with the given phase to a qubit.

Parameters:
  • b (GraphRepresentation) – The graph representation.

  • qubit (int) – The qubit index.

  • phase (Fraction) – Phase in units of Ï€.