APIs

Base.matchMethod
match(
    r::ZXCalculus.ZX.AbstractRule,
    _::ZXCalculus.ZX.AbstractZXDiagram{T, P}
) -> Array{ZXCalculus.ZX.Match{_A}, 1} where _A

Find all vertices in ZX-diagram zxd that match the pattern of rule r.

Returns a vector of Match objects containing the matched vertex sets.

source
Base.replace!Method
replace!(
    r::ZXCalculus.ZX.AbstractRule,
    zxd::ZXCalculus.ZX.AbstractZXDiagram
) -> ZXCalculus.ZX.AbstractZXDiagram

Match and replace with the rule r.

Returns the modified ZX-diagram.

source
Graphs.neMethod
ne(zxd; count_mul = false)

Returns the number of edges of a ZX-diagram. If count_mul, it will return the sum of multiplicities of all multiple edges. Otherwise, it will return the number of multiple edges.

source
Graphs.neighborsMethod
neighbors(zxd, v; count_mul = false)

Returns a vector of vertices connected to v. If count_mul, there will be multiple copy for each vertex. Otherwise, each vertex will only appear once.

source
Graphs.nvMethod
nv(zxd)

Returns the number of vertices (spiders) of a ZX-diagram.

source
ZXCalculus.Utils.add_power!Method
add_power!(
    _::ZXCalculus.ZX.AbstractZXDiagram,
    n
) -> ZXCalculus.ZX.ZXDiagram

Add n to the power of √2 in the global scalar.

source
ZXCalculus.ZX.add_spider!Method
add_spider!(
    _::ZXCalculus.ZX.AbstractZXDiagram,
    stype,
    args...
) -> Any

Add a new spider with spider type st and phase p to the ZX-diagram.

Returns the vertex identifier of the newly added spider.

source
ZXCalculus.ZX.add_spider!Method
add_spider!(zxd, spider_type, phase = 0, connect = [])

Add a new spider which is of the type spider_type with phase phase and connected to the vertices connect.

source
ZXCalculus.ZX.ancilla_extractionMethod
ancilla_extraction(
    zxg::Union{ZXCalculus.ZX.ZXCircuit, ZXCalculus.ZX.ZXGraph}
) -> ZXCalculus.ZX.ZXCircuit{Int64, ZXCalculus.Utils.Phase}

Extract a quantum circuit from a general ZX-graph even without a gflow.

This function can handle ZX-graphs that don't have a generalised flow (gflow), and will introduce post-selection operators as needed.

Returns a ZXCircuit representing the extracted circuit.

source
ZXCalculus.ZX.check_ruleMethod
check_rule(
    r::ZXCalculus.ZX.AbstractRule,
    _::ZXCalculus.ZX.AbstractZXDiagram{T, P},
    _::Array{T, 1}
) -> Bool

Check whether the vertices vs in ZX-diagram zxd still match the rule r.

This is used to verify that a previously matched pattern is still valid before rewriting, as the diagram may have changed since the match was found.

Returns true if the vertices still match the rule pattern, false otherwise.

source
ZXCalculus.ZX.circuit_extractionMethod
circuit_extraction(
    zxg::Union{ZXCalculus.ZX.ZXCircuit{T, P}, ZXCalculus.ZX.ZXGraph{T, P}}
) -> YaoHIR.Chain

Extract a quantum circuit from a graph-like ZX-diagram.

This is the main circuit extraction algorithm that converts a ZX-diagram in graph-like form into an equivalent quantum circuit.

Returns a ZXDiagram representing the extracted circuit.

source
ZXCalculus.ZX.clifford_simplificationMethod
clifford_simplification(
    circ::ZXCalculus.ZX.ZXDiagram
) -> ZXCalculus.ZX.ZXCircuit{T, P} where {T<:Integer, P<:ZXCalculus.Utils.AbstractPhase}

Simplify zxd with the Clifford simplification algorithms in arXiv:1902.03178.

This applies a sequence of local complementation and pivot rules to simplify the ZX-diagram while preserving Clifford structure.

Returns the simplified ZX-diagram.

source
ZXCalculus.ZX.column_locMethod
column_loc(
    _::ZXCalculus.ZX.AbstractZXCircuit,
    v
) -> Union{Nothing, Rational{Int64}}

Get the column (time/depth) location of spider v in the circuit layout.

Returns a rational number representing the position along the circuit, or nothing if not in layout.

source
ZXCalculus.ZX.column_locMethod
column_loc(
    layout::ZXCalculus.ZX.ZXLayout{T},
    v
) -> Union{Nothing, Rational{Int64}}

Return the column number corresponding to the spider v, or nothing if not in layout.

source
ZXCalculus.ZX.concat!Method
concat!(
    circ1::ZXCalculus.ZX.ZXCircuit{T, P},
    circ2::ZXCalculus.ZX.ZXCircuit{T, P}
) -> ZXCalculus.ZX.ZXCircuit

Concatenate two ZX-circuits, where the second circuit is appended after the first.

The circuits must have the same number of qubits. The outputs of the first circuit are connected to the inputs of the second circuit.

Returns the modified first circuit.

source
ZXCalculus.ZX.concat!Method
concat!(
    zxd_1::ZXCalculus.ZX.ZXDiagram{T, P},
    zxd_2::ZXCalculus.ZX.ZXDiagram{T, P}
) -> ZXCalculus.ZX.ZXDiagram

Appends two diagrams, where the second diagram is inverted

source
ZXCalculus.ZX.convert_to_zx_circuitMethod
convert_to_zx_circuit(
    root::YaoHIR.BlockIR
) -> ZXCalculus.ZX.ZXCircuit{Int64, ZXCalculus.Utils.Phase}

Convert a BlockIR to a ZXCircuit.

This is the main conversion function from YaoHIR's BlockIR representation to ZXCalculus. It translates each gate operation in the BlockIR to the corresponding ZX-diagram representation in a ZXCircuit.

Arguments

  • root::YaoHIR.BlockIR: The BlockIR representation to convert

Returns

  • ZXCircuit: The circuit representation in ZX-calculus form

Examples

using YaoHIR, ZXCalculus

# Create a BlockIR from a quantum circuit
bir = BlockIR(...)

# Convert to ZXCircuit (two equivalent ways)
zxc = convert_to_zx_circuit(bir)
zxc = ZXCircuit(bir)  # Constructor syntax

# Convert back to YaoHIR Chain
chain = convert_to_chain(zxc)

See also: ZXCircuit, convert_to_chain

source
ZXCalculus.ZX.convert_to_zxdMethod
convert_to_zxd(
    root::YaoHIR.BlockIR
) -> ZXCalculus.ZX.ZXDiagram{Int64, ZXCalculus.Utils.Phase}

Convert a BlockIR to a ZXDiagram.

Deprecated

convert_to_zxd is deprecated. Use convert_to_zx_circuit or ZXCircuit(bir) instead. This function internally converts to ZXCircuit and then wraps it in deprecated ZXDiagram.

Returns a ZXDiagram for backward compatibility.

source
ZXCalculus.ZX.daggerMethod
dagger(
    circ::ZXCalculus.ZX.ZXCircuit{T, P}
) -> ZXCalculus.ZX.ZXCircuit{_A, _B} where {_B, _A}

Compute the dagger (adjoint) of a ZX-circuit.

This swaps inputs and outputs and negates all phases. The dagger operation corresponds to the adjoint of the quantum operation represented by the circuit.

Returns a new ZX-circuit representing the adjoint operation.

source
ZXCalculus.ZX.daggerMethod
dagger(
    zxd::ZXCalculus.ZX.ZXDiagram{T, P}
) -> ZXCalculus.ZX.ZXDiagram

Dagger of a ZXDiagram by swapping input and outputs and negating the values of the phases

source
ZXCalculus.ZX.edge_typeMethod
edge_type(
    _::ZXCalculus.ZX.AbstractZXDiagram,
    v1,
    v2
) -> ZXCalculus.ZX.EdgeType.EType

Return the edge type between vertices v1 and v2 in the ZX-diagram.

source
ZXCalculus.ZX.find_inputsMethod
find_inputs(zxg::ZXGraph)

Find all spiders with type SpiderType.In in the graph. This is a search utility and does not guarantee circuit structure or ordering.

source
ZXCalculus.ZX.find_outputsMethod
find_outputs(zxg::ZXGraph)

Find all spiders with type SpiderType.Out in the graph. This is a search utility and does not guarantee circuit structure or ordering.

source
ZXCalculus.ZX.gaussian_eliminationMethod
gaussian_elimination(M[, steps])

Return result and steps of Gaussian elimination of matrix M. Here we assume that the elements of M is in binary field F_2 = {0,1}.

source
ZXCalculus.ZX.generate_layout!Method
generate_layout!(
    _::ZXCalculus.ZX.AbstractZXCircuit
) -> ZXCalculus.ZX.ZXLayout

Generate or update the layout for the circuit.

This computes the spatial positioning (qubit and column locations) of all spiders for visualization and analysis purposes.

Returns a ZXLayout object containing the layout information.

source
ZXCalculus.ZX.get_inputsMethod
get_inputs(
    _::ZXCalculus.ZX.AbstractZXCircuit
) -> Vector{T} where T<:Integer

Get the ordered input spider vertices of the circuit.

Returns a vector of vertex identifiers in qubit order.

source
ZXCalculus.ZX.get_outputsMethod
get_outputs(
    _::ZXCalculus.ZX.AbstractZXCircuit
) -> Vector{T} where T<:Integer

Get the ordered output spider vertices of the circuit.

Returns a vector of vertex identifiers in qubit order.

source
ZXCalculus.ZX.import_edges!Method
import_edges!(
    circ1::ZXCalculus.ZX.ZXCircuit{T, P},
    circ2::ZXCalculus.ZX.ZXCircuit{T, P},
    v2tov1::Dict{T, T}
)

Import edges of circ2 to circ1, modifying circ1.

source
ZXCalculus.ZX.import_edges!Method
import_edges!(
    d1::ZXCalculus.ZX.ZXDiagram{T, P},
    d2::ZXCalculus.ZX.ZXDiagram{T, P},
    v2tov1::Dict{T, T}
)

Import edges of d2 to d1, modify d1

source
ZXCalculus.ZX.import_non_in_out!Method
import_non_in_out!(
    circ1::ZXCalculus.ZX.ZXCircuit{T, P},
    circ2::ZXCalculus.ZX.ZXCircuit{T, P},
    v2tov1::Dict{T, T}
)

Add non-input and non-output spiders of circ2 to circ1, modifying circ1.

Records the mapping of vertex indices from circ2 to circ1.

source
ZXCalculus.ZX.import_non_in_out!Method
import_non_in_out!(
    d1::ZXCalculus.ZX.ZXDiagram{T, P},
    d2::ZXCalculus.ZX.ZXDiagram{T, P},
    v2tov1::Dict{T, T}
)

Add non input and output spiders of d2 to d1, modify d1. Record the mapping of vertex indices.

source
ZXCalculus.ZX.insert_spider!Method
insert_spider!(
    _::ZXCalculus.ZX.AbstractZXDiagram,
    v1,
    v2,
    stype,
    args...
) -> Vector{K} where K<:Integer

Insert a new spider on the edge between vertices v1 and v2.

Returns the vertex identifier of the newly inserted spider.

source
ZXCalculus.ZX.insert_spider!Method
insert_spider!(zxd, v1, v2, spider_type, phase = 0)

Insert a spider of the type spider_type with phase = phase, between two vertices v1 and v2. It will insert multiple times if the edge between v1 and v2 is a multiple edge. Also it will remove the original edge between v1 and v2.

source
ZXCalculus.ZX.is_hadamardMethod
is_hadamard(
    zxg::ZXCalculus.ZX.AbstractZXDiagram,
    v1,
    v2
) -> Bool

Return true if the edge between vertices v1 and v2 is a Hadamard edge in the ZX-diagram.

source
ZXCalculus.ZX.ninMethod
nin(zxd::ZXCalculus.ZX.AbstractZXDiagram) -> Any

Get the number of input spiders in the circuit.

source
ZXCalculus.ZX.noutMethod
nout(zxd::ZXCalculus.ZX.AbstractZXDiagram) -> Any

Get the number of output spiders in the circuit.

source
ZXCalculus.ZX.nqubitsMethod
nqubits(_::ZXCalculus.ZX.AbstractZXCircuit) -> Int64

Get the number of qubits in the circuit.

Returns an integer representing the qubit count.

source
ZXCalculus.ZX.phaseMethod
phase(
    _::ZXCalculus.ZX.AbstractZXDiagram,
    v
) -> ZXCalculus.Utils.AbstractPhase

Get the phase of spider v in the ZX-diagram.

Returns a phase value (typically AbstractPhase).

source
ZXCalculus.ZX.phaseMethod
phase(zxd, v)

Returns the phase of a spider. If the spider is not a Z or X spider, then return 0.

source
ZXCalculus.ZX.phase_teleportationMethod
phase_teleportation(
    cir::ZXCalculus.ZX.ZXDiagram
) -> ZXCalculus.ZX.ZXDiagram

Reduce T-count of zxd with the phase teleportation algorithms in arXiv:1903.10477.

This optimization technique reduces the number of non-Clifford (T) gates in the circuit by teleporting phases through the diagram.

Returns a ZX-diagram with reduced T-count.

source
ZXCalculus.ZX.phasesMethod
phases(
    _::ZXCalculus.ZX.AbstractZXDiagram
) -> Dict{T, P} where {T<:Integer, P<:ZXCalculus.Utils.AbstractPhase}

Get all spider phases in the ZX-diagram.

Returns a dictionary mapping vertex identifiers to their phases.

source
ZXCalculus.ZX.push_gate!Method
push_gate!(
    _::ZXCalculus.ZX.AbstractZXCircuit,
    args...
) -> ZXCalculus.ZX.ZXCircuit{T, P} where {T<:Integer, P<:ZXCalculus.Utils.AbstractPhase}

Add a gate to the end of the circuit.

source
ZXCalculus.ZX.push_gate!Method
push_gate!(
    _::ZXCalculus.ZX.ZXCircuit{T, P},
    gate::Val{G},
    args...
) -> ZXCalculus.ZX.ZXCircuit

Add a gate to the end of the circuit.

Arguments

  • circ: The circuit to modify
  • gate: Gate type as Val (e.g., Val(:H), Val(:CNOT))
  • locs: Qubit locations where the gate is applied
  • phase: Optional phase parameter for phase gates (:Z, :X)

Supported gates

  • Single-qubit: :Z, :X, :H
  • Two-qubit: :CNOT, :CZ, :SWAP
source
ZXCalculus.ZX.push_gate!Method
push_gate!(zxd, ::Val{M}, locs...[, phase]; autoconvert=true)

Push an M gate to the end of qubit loc where M can be :Z, :X, :H, :SWAP, :CNOT and :CZ. If M is :Z or :X, phase will be available and it will push a rotation M gate with angle phase * π. If autoconvert is false, the input phase should be a rational numbers.

source
ZXCalculus.ZX.pushfirst_gate!Method
pushfirst_gate!(
    _::ZXCalculus.ZX.AbstractZXCircuit,
    args...
) -> ZXCalculus.ZX.ZXCircuit{T, P} where {T<:Integer, P<:ZXCalculus.Utils.AbstractPhase}

Add a gate to the beginning of the circuit.

source
ZXCalculus.ZX.pushfirst_gate!Method
pushfirst_gate!(
    _::ZXCalculus.ZX.ZXCircuit{T, P},
    gate::Val{G},
    args...
) -> ZXCalculus.ZX.ZXCircuit

Add a gate to the beginning of the circuit.

Arguments

  • circ: The circuit to modify
  • gate: Gate type as Val (e.g., Val(:H), Val(:CNOT))
  • locs: Qubit locations where the gate is applied
  • phase: Optional phase parameter for phase gates (:Z, :X)

Supported gates

  • Single-qubit: :Z, :X, :H
  • Two-qubit: :CNOT, :CZ, :SWAP
source
ZXCalculus.ZX.pushfirst_gate!Method
pushfirst_gate!(zxd, ::Val{M}, loc[, phase])

Push an M gate to the beginning of qubit loc where M can be :Z, :X, :H, :SWAP, :CNOT and :CZ. If M is :Z or :X, phase will be available and it will push a rotation M gate with angle phase * π.

source
ZXCalculus.ZX.qubit_locMethod
qubit_loc(
    _::ZXCalculus.ZX.AbstractZXCircuit,
    v
) -> Union{Nothing, Rational{Int64}}

Get the qubit (row) location of spider v in the circuit layout.

Returns an integer representing the qubit wire index (1 to nqubits), or nothing if not in layout.

source
ZXCalculus.ZX.qubit_locMethod
qubit_loc(
    layout::ZXCalculus.ZX.ZXLayout{T},
    v
) -> Union{Nothing, Rational{Int64}}

Return the qubit number corresponding to the spider v, or nothing if not in layout.

source
ZXCalculus.ZX.rewrite!Method
rewrite!(
    r::ZXCalculus.ZX.AbstractRule,
    zxd::ZXCalculus.ZX.AbstractZXDiagram{T, P},
    matches::Array{ZXCalculus.ZX.Match{T}, 1}
) -> ZXCalculus.ZX.AbstractZXDiagram{T, P} where {T, P}

Rewrite a ZX-diagram zxd with rule r for all vertices in matches.

The matches parameter can be a vector of Match objects or a single Match instance.

Returns the modified ZX-diagram.

source
ZXCalculus.ZX.rewrite!Method
rewrite!(
    r::ZXCalculus.ZX.AbstractRule,
    _::ZXCalculus.ZX.AbstractZXDiagram{T, P},
    _::Array{T, 1}
) -> ZXCalculus.ZX.ZXGraph

Rewrite a ZX-diagram zxd with rule r for the specific vertices vs.

This is the core rewriting function that must be implemented for each rule.

Returns the modified ZX-diagram.

source
ZXCalculus.ZX.scalarMethod
scalar(
    _::ZXCalculus.ZX.AbstractZXDiagram
) -> ZXCalculus.Utils.Scalar{P} where P<:ZXCalculus.Utils.AbstractPhase

Get the global scalar of the ZX-diagram.

Returns a Scalar object containing the phase and power of √2.

source
ZXCalculus.ZX.set_column!Method
set_column!(
    layout::ZXCalculus.ZX.ZXLayout{T},
    v,
    col
) -> ZXCalculus.ZX.ZXLayout{T} where T

Set the column number of the spider v to col.

source
ZXCalculus.ZX.set_edge_type!Method
set_edge_type!(
    _::ZXCalculus.ZX.AbstractZXDiagram,
    v1,
    v2,
    etype
) -> Bool

Set the edge type between vertices v1 and v2 in the ZX-diagram.

source
ZXCalculus.ZX.set_loc!Method
set_loc!(
    layout::ZXCalculus.ZX.ZXLayout{T},
    v,
    q,
    col
) -> ZXCalculus.ZX.ZXLayout{T} where T

Set both the qubit and column location of the spider v.

source
ZXCalculus.ZX.set_phase!Method
set_phase!(_::ZXCalculus.ZX.AbstractZXDiagram, v, p) -> Bool

Set the phase of spider v to p in the ZX-diagram.

source
ZXCalculus.ZX.set_qubit!Method
set_qubit!(
    layout::ZXCalculus.ZX.ZXLayout{T},
    v,
    q
) -> ZXCalculus.ZX.ZXLayout{T} where T

Set the qubit number of the spider v to q.

source
ZXCalculus.ZX.simplify!Method
simplify!(
    r::ZXCalculus.ZX.AbstractRule,
    zxd::ZXCalculus.ZX.AbstractZXDiagram
) -> ZXCalculus.ZX.AbstractZXDiagram

Simplify zxd with the rule r.

Repeatedly applies the rule until no more matches are found or MAX_ITERATION is reached.

Returns the simplified ZX-diagram.

source
ZXCalculus.ZX.spider_sequenceMethod
spider_sequence(
    _::ZXCalculus.ZX.AbstractZXCircuit
) -> Vector{Any}

Get the ordered sequence of spiders for each qubit.

Returns a vector of vectors, where each inner vector contains the spider vertices on a particular qubit wire, ordered by their column position.

source
ZXCalculus.ZX.spider_typeMethod
spider_type(
    _::ZXCalculus.ZX.AbstractZXDiagram,
    v
) -> ZXCalculus.ZX.SpiderType.SType

Get the type of spider v in the ZX-diagram.

Returns a SpiderType.SType value (Z, X, H, In, or Out).

source
ZXCalculus.ZX.spider_typesMethod
spider_types(
    _::ZXCalculus.ZX.AbstractZXDiagram
) -> Dict{T, ZXCalculus.ZX.SpiderType.SType} where T<:Integer

Get all spider types in the ZX-diagram.

Returns a dictionary mapping vertex identifiers to their spider types.

source
ZXCalculus.ZX.spidersMethod
spiders(
    _::ZXCalculus.ZX.AbstractZXDiagram
) -> Vector{K} where K<:Integer

Get all spider vertices in the ZX-diagram.

Returns a vector of vertex identifiers.

source
ZXCalculus.ZX.tcountMethod
tcount(_::ZXCalculus.ZX.AbstractZXDiagram) -> Any

Count the number of non-Clifford phases (T-gates) in the ZX-diagram.

Returns an integer count.

source
ZXCalculus.ZX.verify_equalityMethod
verify_equality(
    circ_1::ZXCalculus.ZX.ZXCircuit,
    circ_2::ZXCalculus.ZX.ZXCircuit
) -> Bool

Check the equivalence of two different ZX-circuits.

This function concatenates the first circuit with the dagger of the second, then performs full reduction. If the result contains only bare wires, the circuits are equivalent.

Returns true if the circuits are equivalent, false otherwise.

source
ZXCalculus.ZX.verify_equalityMethod
verify_equality(
    zxd_1::ZXCalculus.ZX.ZXDiagram,
    zxd_2::ZXCalculus.ZX.ZXDiagram
) -> Bool

Check the equivalence of two different ZX-diagrams.

This function concatenates the first diagram with the dagger of the second, then performs full reduction. If the result contains only bare wires, the diagrams are equivalent.

Returns true if the diagrams are equivalent, false otherwise.

source
ZXCalculus.ZX.AbstractZXCircuitType
abstract type AbstractZXCircuit{T<:Integer, P<:ZXCalculus.Utils.AbstractPhase} <: ZXCalculus.ZX.AbstractZXDiagram{T<:Integer, P<:ZXCalculus.Utils.AbstractPhase}

Abstract type for ZX-diagrams with circuit structure.

This type represents ZX-diagrams that have explicit quantum circuit semantics, including ordered inputs, outputs, and layout information for visualization.

Interface Requirements

Concrete subtypes must implement both:

  1. The AbstractZXDiagram interfaces (graphinterface, calculusinterface)

  2. The circuit-specific interfaces defined here:

    • circuit_interface.jl: Circuit structure and gate operations
    • layout_interface.jl: Layout information for visualization

Use Interfaces.test to verify implementations.

See also

  • AbstractZXDiagram: Base abstract type for all ZX-diagrams
  • ZXCircuit: Main implementation with ZXGraph composition
  • ZXGraph: Pure graph representation without circuit assumptions
source
ZXCalculus.ZX.AbstractZXDiagramType
abstract type AbstractZXDiagram{T<:Integer, P<:ZXCalculus.Utils.AbstractPhase}

Abstract type for ZX-diagrams, representing graph-like quantum circuit diagrams.

This type defines the base interface for all ZX-diagram representations, providing graph operations and spider (vertex) manipulation methods.

Interface Requirements

Concrete subtypes must implement the following interfaces:

  • graph_interface.jl: Graph operations (Graphs.jl compatibility)
  • calculus_interface.jl: Spider and scalar operations (ZX-calculus)

Use Interfaces.test to verify implementations.

See also: AbstractZXCircuit, ZXGraph, ZXCircuit

source
ZXCalculus.ZX.IdentityRemovalRuleType
struct IdentityRemovalRule <: ZXCalculus.ZX.AbstractRule

Removes identity spiders connected to two spiders or a Pauli spider connected to one Z-spider via a Hadamard edge.

source
ZXCalculus.ZX.LocalCompRuleType
struct LocalCompRule <: ZXCalculus.ZX.AbstractRule

Remove a Z-spider with Clifford phase by local complementary. Requirements:

  • The spider must be interior (not connected to inputs/outputs)
  • The spider must have Clifford phase (±pi/2)
  • All edges connected to the spider must be hadamard edges.
source
ZXCalculus.ZX.MatchType
struct Match{T<:Integer}

A struct for saving matched vertices from rule matching.

Fields

  • vertices::Vector{T} where T<:Integer: Vector of vertex identifiers that match a rule pattern
source
ZXCalculus.ZX.Pivot1RuleType
struct Pivot1Rule <: ZXCalculus.ZX.AbstractRule

Remove two adjacent Pauli Z spiders by pivoting. Some requirements:

  • Both spiders must be interior (not connected to inputs/outputs)
  • Both spiders must have Pauli phases (0 or pi)
  • The two spiders must be connected by an hadamard edge.
source
ZXCalculus.ZX.Pivot2RuleType
struct Pivot2Rule <: ZXCalculus.ZX.AbstractRule

The pivoting rule that convert an internal non-Clifford u Z-spider connected to an internal Pauli Z-spider v into a phase gadget. Requirements:

  • The internal non-Clifford spider must have at least two neighbors (not a part of the phase gadget).
  • The internal Pauli spider must be connected to the internal non-Clifford spider via a Hadamard edge.
source
ZXCalculus.ZX.Pivot3RuleType
struct Pivot3Rule <: ZXCalculus.ZX.AbstractRule

The pivoting rule that convert a boundary non-Clifford u Z-spider connected to an internal Pauli Z-spider v into a phase gadget. Requirements:

- The boundary non-Clifford spider must have at least two neighbors (otherwise should be extracted directly).
- The internal Pauli spider must be connected to the boundary non-Clifford spider via a Hadamard edge.
source
ZXCalculus.ZX.PivotBoundaryRuleType
struct PivotBoundaryRule <: ZXCalculus.ZX.AbstractRule

Applying pivoting rule when an internal Pauli Z-spider u is connected to a Z-spider v on the boundary via a Hadamard edge.

source
ZXCalculus.ZX.PivotGadgetRuleType
struct PivotGadgetRule <: ZXCalculus.ZX.AbstractRule
  • This rule should be only used in circuit extraction.
  • This rule will do pivoting on u, v but preserve u, v.
  • And the scalars are not considered in this rule.
  • gadget_u is the non-Clifford spider
source
ZXCalculus.ZX.RuleType
Rule{L}

The struct for identifying different rules.

Rule for ZXDiagrams:

  • FusionRule(): fusion rule (also available as Rule{:f}())
  • XToZRule(): hadamard rule (also available as Rule{:h}())
  • Identity1Rule(): identity rule 1 (also available as Rule{:i1}())
  • HBoxRule(): identity rule 2 (also available as Rule{:i2}())
  • PiRule(): π rule (also available as Rule{:pi}())
  • CopyRule(): copy rule (also available as Rule{:c}())
  • BialgebraRule(): bialgebra rule (also available as Rule{:b}())
  • ScalarRule(): scalar rule (also available as Rule{:scalar}())

Rule for ZXGraphs:

  • LocalCompRule(): local complementary rule (also available as Rule{:lc}())
  • Pivot1Rule(): pivoting rule (also available as Rule{:p1}())
  • PivotBoundaryRule(): rule for removing Pauli spiders adjacent to boundary spiders (also available as Rule{:pab}())
  • Pivot2Rule(): pivot rule 2 (also available as Rule{:p2}())
  • Pivot3Rule(): pivot rule 3 (also available as Rule{:p3}())
  • IdentityRemovalRule(): identity removal rule (also available as Rule{:id}())
  • GadgetFusionRule(): gadget fusion rule (also available as Rule{:gf}())
  • PivotGadgetRule(): pivot gadget rule (also available as Rule{:pivot}())
  • ScalarRule(): scalar rule (also available as Rule{:scalar}())
source
ZXCalculus.ZX.ZXCircuitType
struct ZXCircuit{T, P} <: ZXCalculus.ZX.AbstractZXCircuit{T, P}

This is the type for representing ZX-circuits with explicit circuit structure.

ZXCircuit separates the graph representation (ZXGraph) from circuit semantics (ordered inputs/outputs and layout). This design enables efficient graph-based simplification while maintaining circuit structure for extraction and visualization.

Fields

  • zx_graph::ZXCalculus.ZX.ZXGraph{T, P} where {T, P}: The underlying ZXGraph representation

  • inputs::Vector: Ordered input spider vertices

  • outputs::Vector: Ordered output spider vertices

  • layout::ZXCalculus.ZX.ZXLayout{T} where T: Layout information for visualization

  • phase_ids::Dict{T, Tuple{T, Int64}} where T: Maps vertex id to its master id and scalar multiplier for phase tracking

  • master::Union{Nothing, ZXCalculus.ZX.ZXCircuit{T, P}} where {T, P}: Reference to master circuit for phase tracking (optional)

source
ZXCalculus.ZX.ZXCircuitMethod
ZXCircuit(
    nbits::Integer
) -> ZXCalculus.ZX.ZXCircuit{T, ZXCalculus.Utils.Phase} where T<:Integer

Construct an empty ZX-circuit with nbits qubits.

Each qubit is represented by a pair of In/Out spiders connected by a simple edge. This creates a minimal circuit structure ready for gate insertion.

Example

julia> circ = ZXCircuit(3)
ZXCircuit with 3 inputs and 3 outputs...
source
ZXCalculus.ZX.ZXDiagramType
struct ZXDiagram{T<:Integer, P<:ZXCalculus.Utils.AbstractPhase} <: ZXCalculus.ZX.AbstractZXCircuit{T<:Integer, P<:ZXCalculus.Utils.AbstractPhase}

This is the type for representing ZX-diagrams.

Deprecated

ZXDiagram is deprecated and will be removed in a future version. Please use ZXCircuit instead for circuit representations.

ZXCircuit provides the same functionality with better separation of concerns and more efficient graph-based simplification algorithms.

Fields

  • mg::Multigraph: The underlying multigraph structure

  • st::Dict{T, ZXCalculus.ZX.SpiderType.SType} where T<:Integer: Spider types indexed by vertex

  • ps::Dict{T, P} where {T<:Integer, P<:ZXCalculus.Utils.AbstractPhase}: Phases of spiders indexed by vertex

  • layout::ZXCalculus.ZX.ZXLayout: Layout information for visualization

  • phase_ids::Dict{T, Tuple{T, Int64}} where T<:Integer: Maps vertex id to its master id and scalar multiplier

  • scalar::ZXCalculus.Utils.Scalar{P} where P<:ZXCalculus.Utils.AbstractPhase: Global scalar factor

  • inputs::Vector{T} where T<:Integer: Ordered input spider vertices

  • outputs::Vector{T} where T<:Integer: Ordered output spider vertices

source
ZXCalculus.ZX.ZXDiagramMethod
ZXDiagram(nbits)
Deprecated

ZXDiagram is deprecated. Use ZXCircuit instead.

Construct a ZXDiagram of a empty circuit with qubit number nbit

julia> zxd = ZXDiagram(3)
ZX-diagram with 6 vertices and 3 multiple edges:
(S_1{input} <-1-> S_2{output})
(S_3{input} <-1-> S_4{output})
(S_5{input} <-1-> S_6{output})
source
ZXCalculus.ZX.ZXDiagramMethod
ZXDiagram(mg::Multigraph{T}, st::Dict{T, SpiderType.SType}, ps::Dict{T, P},
    layout::ZXLayout{T} = ZXLayout{T}(),
    phase_ids::Dict{T,Tuple{T, Int}} = Dict{T,Tuple{T,Int}}()) where {T, P}
ZXDiagram(mg::Multigraph{T}, st::Vector{SpiderType.SType}, ps::Vector{P},
    layout::ZXLayout{T} = ZXLayout{T}()) where {T, P}

Construct a ZXDiagram with all information.

source
ZXCalculus.ZX.ZXGraphType
struct ZXGraph{T<:Integer, P<:ZXCalculus.Utils.AbstractPhase} <: ZXCalculus.ZX.AbstractZXDiagram{T<:Integer, P<:ZXCalculus.Utils.AbstractPhase}

This is the type for representing the graph-like ZX-diagrams.

A pure graph representation without circuit structure assumptions. Spiders of type In/Out can exist but are treated as searchable vertices, not as ordered inputs/outputs.

Fields

  • mg::Multigraph: The underlying multigraph structure

  • ps::Dict{T, P} where {T<:Integer, P<:ZXCalculus.Utils.AbstractPhase}: Phases of spiders indexed by vertex

  • st::Dict{T, ZXCalculus.ZX.SpiderType.SType} where T<:Integer: Spider types indexed by vertex

  • et::Dict{Tuple{T, T}, ZXCalculus.ZX.EdgeType.EType} where T<:Integer: Edge types indexed by sorted vertex pairs

  • scalar::ZXCalculus.Utils.Scalar{P} where P<:ZXCalculus.Utils.AbstractPhase: Global scalar factor

source
ZXCalculus.ZX.ZXLayoutType
struct ZXLayout{T<:Integer}

A struct for the layout information of ZX-circuits.

Fields

  • nbits::Int64: Number of qubits in the circuit

  • spider_q::Dict{T, Rational{Int64}} where T<:Integer: Mapping from spider vertices to qubit locations

  • spider_col::Dict{T, Rational{Int64}} where T<:Integer: Mapping from spider vertices to column positions

source
Base.matchMethod
match(r, zxwd)

Returns all matched vertices, which will be store in sturct Match, for rule r in a ZXW-diagram zxwd.

source
Graphs.neMethod
ne(zxwd; count_mul = false)

Returns the number of edges of a ZXW-diagram. If count_mul, it will return the sum of multiplicities of all multiple edges. Otherwise, it will return the number of multiple edges.

source
Graphs.neighborsMethod
neighbors(zxwd, v; count_mul = false)

Returns a vector of vertices connected to v. If count_mul, there will be multiple copy for each vertex. Otherwise, each vertex will only appear once.

source
Graphs.nvMethod
nv(zxwd)

Returns the number of vertices (spiders) of a ZXW-diagram.

source
ZXCalculus.ZX.push_gate!Method
push_gate!(zxwd, ::Val{M}, locs...[, phase]; autoconvert=true)

Push an M gate to the end of qubit loc where M can be :Z, :X, :H, :SWAP, :CNOT and :CZ. If M is :Z or :X, phase will be available and it will push a rotation M gate with angle phase * π. If autoconvert is false, the input phase should be a rational numbers.

source
ZXCalculus.ZX.pushfirst_gate!Method
pushfirst_gate!(zxwd, ::Val{M}, loc[, phase])

Push an M gate to the beginning of qubit loc where M can be :Z, :X, :H, :SWAP, :CNOT and :CZ. If M is :Z or :X, phase will be available and it will push a rotation M gate with angle phase * π.

source
ZXCalculus.ZX.rewrite!Method
rewrite!(r, zxd, matches)

Rewrite a ZX-diagram zxd with rule r for all vertices in matches. matches can be a vector of Match or just an instance of Match.

source
ZXCalculus.ZXW.add_spider!Method
add_spider!(zxwd, spider, connect = [])

Add a new spider spider with appropriate parameter connected to the vertices connect.

source
ZXCalculus.ZXW.concat!Method

Concatenate two ZXWDiagrams, modify d1.

Remove outputs of d1 and inputs of d2. Then add edges between to vertices that was conntecting to outputs of d1 and inputs of d2. Assuming you don't concatenate two empty circuit ZXWDiagram

source
ZXCalculus.ZXW.insert_spider!Method
insert_spider!(zxwd, v1, v2, spider)

Insert a spider spider with appropriate parameter, between two vertices v1 and v2. It will insert multiple times if the edge between v1 and v2 is a multiple edge. Also it will remove the original edge between v1 and v2.

source
ZXCalculus.ZXW.int_prep!Method

Prepare spider at loc for integration.

Perform the simplified step of zeroing out phase of spider and readying it for integration

  1. If target spider is X spider, turn it to Z by adding H to all its legs
  2. Pull out the Phase of the spider
  3. zero out the phase
  4. change the current spider back to its original type if necessary,

will generate one extra H spider.

source
ZXCalculus.ZXW.set_phase!Method
set_phase!(zxwd, v, p)

Set the phase of v in zxwd to p. If v is not a Z or X spider, then do nothing. If v is not in zxwd, then return false to indicate failure.

source
ZXCalculus.ZXW.stack_zxwd!Method

Stacking two ZXWDiagrams in place. Modify d1.

Performs tensor product of two ZXWDiagrams. The result is a ZXWDiagram with d1 on lower qubit indices. Assuming number of inputs and outputs of are the same for both d1 and d2.

source
Graphs.neMethod
ne(zwd)

Returns the number of edges of a ZW-diagram.

source
Graphs.nvMethod
nv(zwd)

Returns the number of vertices (spiders) of a ZW-diagram.

source
ZXCalculus.ZW.add_spider!Method
add_spider!(zwd, spider, connect = [])

Add a new spider spider with appropriate parameter connected to the half edges connect.

Had to make halfedge class 1 citizen because there will be ambiguity Consider A to B and there are multiple edges to A and from A to B

source
ZXCalculus.ZW.insert_spider!Method
insert_spider!(zwd, he1, spider)

Insert a spider spider with appropriate parameter on the half-edge prior to he1. v1 <- he1 - v2 becomes v1 <- he1 - v2 <- henew - vnew

source
ZXCalculus.ZW.parameterMethod
parameter(zwd, v)

Returns the parameter of a spider. If the spider is not a monoZ or binZ spider, then return 0.

source
ZXCalculus.ZW.set_phase!Method
set_phase!(zwd, v, p)

Set the phase of v in zwd to p. If v is not a monoZ or binZ spider, then do nothing. If v is not in zwd, then return false to indicate failure.

source
ZXCalculus.ZW.ZWDiagramMethod
ZWDiagram(nbits::T, ::Type{P}) where {T<:Integer}

Create a ZW-diagram with nbits input and output qubits.

Scalar is parameterized to be P.

source
ZXCalculus.Utils.ParameterType
Parameter

The Algebraic Data Type for representing parameter related to spider. PiUnit(x) represents the the phase of a number exp(im*x*π). Factor(x) represents a number x.

source
ZXCalculus.PMG.add_facet_to_boarder!Method
add_facet_to_boarder!(pmg::PlanarMultigraph{T}, h::T, g::T) where {T<:Integer}

Creates a facet with edge connecting the destination of h and g.

h and g needs to be in ccw order

Reference

source
ZXCalculus.PMG.create_edge!Method
create_edge!(pmg::PlanarMultigraph{T}, vs::T, vd::T) where {T<:Integer}

Create an a pair of halfedge from vs to vd, add to PlanarMultigraph pmg. Facet information is not updated yet but set to default value of 0. Vertex to halfedge is updated and set to the two newly added half edges.

source
ZXCalculus.PMG.create_vertex!Method
create_vertex!(g::PlanarMultigraph{T}; mul::Int = 1) where {T<:Integer}

Create vertices.

Create mul of vertices and record them in PlanarMultigraph g's v_max field.

source
ZXCalculus.PMG.gc_vertex!Method
gc_vertex!(pmg::PlanarMultigraph{T}, vs::Vector{T}) where {T<:Integer}

Garbage collect vertices that's no longer connected to any edge.

source
ZXCalculus.PMG.join_facet!Method
join_facet!(pmg::PlanarMultigraph{T}, h::T) where {T}

Join two facets incident to h and it's twin into one.

The facet incident to h's twin is removed.

source
ZXCalculus.PMG.join_vertex!Method
join_vertex!(pmg::PlanarMultigraph{T}, h::T) where {T<:Integer}

Join two vertices connected by a HalfEdge into one.

Provides the removal of an edge.

source
ZXCalculus.PMG.make_hole!Method
make_hole!(pmg::PlanarMultigraph{T}, h::T) where {T<:Integer}

Convert the facet incident to a half edge into a hole.

Makes all the half edges in the facet a boundary halfedge.

Reference

source
ZXCalculus.PMG.prevMethod
prev(g::PlanarMultigraph{T}, he_id::T) where {T<:Integer}

Provides Previous Half Edge of a facet. HDS is bidi

source
ZXCalculus.PMG.split_edge!Method
split_edge!(pmg::PlanarMultigraph{T}, h::T) where {T<:Integer}

Split an edge into two consecutive ones. 1->2->3 becomes 1->4->2->3

source
ZXCalculus.PMG.split_facet!Method
split_facet!(pmg::PlanarMultigraph{T}, h::T, g::T) where {T<:Integer}

Split a facet incident to h and g into two facets.

Precondition

  1. h and g are in the same facet
  2. h and g are not the same half edge
  3. Cannot be used to split the faces incident to the multiedge.
source
ZXCalculus.PMG.split_vertex!Method
split_vertex!(g::PlanarMultigraph{T}, he1::T, he2::T) where {T<:Integer}

Split a vertex into 2 vertices.

Connect the two vertices with a new pair of half edges. he1 and he2 are half edges that marks the start and end of half edges that remain on v1.

After splitting, h points to the newly added vertex

Reference

source
ZXCalculus.PMG.trace_faceMethod
trace_face(g::PlanarMultigraph{T}, f::T; safe_trace = false) where {T}

Return the half edges of a face.

If safe_trace is true, then the half edges are returned in scrambled order. Otherwise, the returned half edges are in counter clockwise order but is not guaranteed to be consitent with he2f.

source
ZXCalculus.PMG.PlanarMultigraphType
PlanarMultigraph{T<:Integer}

Implements a planar multigraph with a maximal HDS Structure.

Features

  1. Stores Forward Half Edge pointer in facet
  2. Vertex linked
  3. Face Linked

References:

Our implementation is based heavily on the CGAL Library. A good introduction on the HDS Structure and the Polyhedron3 Object which gives the boundary representation of a 2-manifold can be found in this paper.

TODO: Proof of Completeness for Euler Operations with Preconditions In the

CGAL Library, the Euler Operations are implemented with preconditions. It was pointed out that Euler Operations are closed for orientable 2-manifolds in paper where the detailed proof is in book. It was further pointed out in paper that Euler Operations are complete in Theorem 4.4.

The question remains whether the completeness remains for the preconditions attached. One way of proving is to show that for the Euler Operations used in Theorem 4.4, we could always construct them out of the Euler Operations in CGAL Library with preconditions?

source