qml.from_qasm

from_qasm(quantum_circuit, measurements=False)[source]

Loads quantum circuits from a QASM string using the converter in the PennyLane-Qiskit plugin.

Parameters
  • quantum_circuit (str) – a QASM string containing a valid quantum circuit

  • measurements (None | MeasurementProcess | list[MeasurementProcess]) – an optional PennyLane measurement or list of PennyLane measurements that overrides any terminal measurements that may be present in the input circuit. If set to None, existing measurements in the input circuit will be used.

Returns

the PennyLane template created based on the QASM string

Return type

function

By default, from_qasm will remove any measurements that are present in the QASM code. If the QASM code contains measurements, set measurements=None to keep them in the output of from_qasm.

Warning

The current default behaviour of removing measurements in the QASM code is deprecated and will be changed in a future release. Starting in version 0.38, from_qasm will keep the measurements from the QASM code by default. To remove all measurements, set measurements=[] which overrides the existing measurements with an empty list.

Example:

>>> hadamard_qasm = 'OPENQASM 2.0;' \
...                 'include "qelib1.inc";' \
...                 'qreg q[1];' \
...                 'creg c[1];' \
...                 'h q[0];'
>>> my_circuit = qml.from_qasm(hadamard_qasm)
>>> my_circuit()
[]

The measurements can also be passed directly to the function when creating the quantum function, making it possible to create a PennyLane circuit with qml.QNode:

>>> measurements = [qml.var(qml.Y(0))]
>>> circuit = qml.QNode(qml.from_qasm(hadamard_qasm, measurements), dev)
>>> circuit()
[tensor(1., requires_grad=True)]

Mid-circuit measurements inside the QASM code can also be interpreted.

hadamard_qasm = 'OPENQASM 2.0;' \
                 'include "qelib1.inc";' \
                 'qreg q[2];' \
                 'creg c[2];' \
                 'h q[0];' \
                 'measure q[0] -> c[0];' \
                 'rz(0.24) q[0];' \
                 'cx q[0], q[1];' \
                 'measure q -> c;'

dev = qml.device("default.qubit")
loaded_circuit = qml.from_qasm(hadamard_qasm, measurements=None)

@qml.qnode(dev)
def circuit():
    mid_measure, m0, m1 = loaded_circuit()
    qml.cond(mid_measure == 0, qml.RX)(np.pi / 2, 0)
    return [qml.expval(qml.measure(0))]
>>> circuit()
[tensor(0.75, requires_grad=True)]

You can also load the contents of a QASM file:

>>> with open("hadamard_circuit.qasm", "r") as f:
...     my_circuit = qml.from_qasm(f.read())

The my_circuit template can now be used within QNodes, as a two-wire quantum template.

>>> @qml.qnode(dev)
>>> def circuit(x):
>>>     qml.RX(x, wires=1)
>>>     my_circuit(wires=(1, 0))
>>>     return qml.expval(qml.Z(0))

Contents

Using PennyLane

Release news

Development

API

Internals