qml.from_qasm3

from_qasm3(quantum_circuit, wire_map=None)[source]

Converts an OpenQASM 3.0 circuit into a quantum function that can be used within a QNode.

Note

The following OpenQASM 3.0 gates are not supported: sdg, tdg, cu. Built-in mathematical functions and constants, custom gates, and pulses are not yet supported. The remaining standard library gates, subroutines, variables, control flow, measurements and end statements are all supported.

In order to use this function, openqasm3 and 'openqasm3[parser]' must be installed in the user’s environment. Please consult the OpenQASM installation instructions for directions.

Parameters:
  • quantum_circuit (str) – a QASM 3.0 string containing a simple quantum circuit.

  • Optional[dict] (qubit_mapping) – the mapping from OpenQASM 3.0 qubit names to PennyLane wires.

Returns:

A quantum function that will execute the program.

Return type:

function

Examples

qasm_string = '''
        qubit q0;
        qubit q1;
        qubit q2;

        float theta = 0.2;
        int power = 2;

        ry(theta / 2) q0;
        rx(theta) q1;
        pow(power) @ x q0;

        def random(qubit q) -> bit
        {
            bit b = "0";
            h q;
            measure q -> b;
            return b;
        }

        bit m = random(q2);

        if (m) {
            int i = 0;
            while (i < 5) {
                i = i + 1;
                rz(i) q1;
                break;
            }
        }
'''
import pennylane as qml

dev = qml.device("default.qubit", wires=[0, 1, 2])
@qml.qnode(dev)
def my_circuit():
    qml.from_qasm3(
        qasm_string,
        {'q0': 0, 'q1': 1, 'q2': 2}
    )()
    return qml.expval(qml.Z(0))
>>> print(qml.draw(my_circuit)())
0: ──RY(0.10)──X²────────────┤  <Z>
1: ──RX(0.20)───────RZ(1.00)─┤
2: ──H─────────┤↗├──║────────┤
                ╚═══╝