qml.transforms.from_zx

from_zx(graph, decompose_phases=True)[source]

Converts a graph from PyZX to a PennyLane tape, if the graph is diagram-like.

Parameters
  • graph (Graph) – ZX graph in PyZX.

  • decompose_phases (bool) – If True the phases are decomposed, meaning that qml.RZ() and qml.RX() are simplified into other gates (e.g. qml.T(), qml.S(), …).

Example

From the example for the to_zx() function, one can convert back the PyZX graph to a PennyLane by using the function from_zx().

import pyzx
dev = qml.device('default.qubit', wires=2)

@qml.transforms.to_zx
def circuit(p):
    qml.RZ(p[0], wires=0),
    qml.RZ(p[1], wires=0),
    qml.RX(p[2], wires=1),
    qml.PauliZ(wires=1),
    qml.RZ(p[3], wires=0),
    qml.PauliX(wires=0),
    qml.CNOT(wires=[1, 0]),
    qml.CNOT(wires=[0, 1]),
    qml.SWAP(wires=[1, 0]),
    return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))

params = [5 / 4 * np.pi, 3 / 4 * np.pi, 0.1, 0.3]
g = circuit(params)

pennylane_tape = qml.transforms.from_zx(g)

You can check that the operations are similar but some were decomposed in the process.

>>> pennylane_tape.operations
[PauliZ(wires=[0]),
 T(wires=[0]),
 RX(0.1, wires=[1]),
 PauliZ(wires=[0]),
 Adjoint(T(wires=[0])),
 PauliZ(wires=[1]),
 RZ(0.3, wires=[0]),
 PauliX(wires=[0]),
 CNOT(wires=[1, 0]),
 CNOT(wires=[0, 1]),
 CNOT(wires=[1, 0]),
 CNOT(wires=[0, 1]),
 CNOT(wires=[1, 0])]

Warning

Be careful because not all graphs are circuit-like, so the process might not be successful after you apply some optimization on your PyZX graph. You can extract a circuit by using the dedicated PyZX function.

Note

It is a PennyLane adapted and reworked graph_to_circuit function.

Copyright (C) 2018 - Aleks Kissinger and John van de Wetering