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 standard library gates, qubit registers, built-in mathematical functions and constants, subroutines, variables, control flow, measurements, inputs, outputs, custom gates and
endstatements are all supported. Pulses are not yet supported.In order to use this function,
openqasm3and'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.
wire_map (Optional[dict]) – the mapping from OpenQASM 3.0 qubit names to PennyLane wires.
- Returns:
A quantum function that will execute the program.
- Return type:
Callable
Examples
First, we define a QASM 3.0 circuit as a string. In this example, we define three qubits, a few parameterized gates, a subroutine with a measurement, and a control flow statement.
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; } } '''
We can convert this circuit into a PennyLane quantum function using:
@qml.qnode(qml.device("default.qubit", wires=[0, 1, 2])) def my_circuit(): qml.from_qasm3( qasm_string, {'q0': 0, 'q1': 1, 'q2': 2} )() return qml.expval(qml.Z(0))
Inspecting the circuit, we can see that the operations and measurements have been correctly interpreted.
>>> print(qml.draw(my_circuit)()) 0: ──RY(0.10)──X²────────────┤ <Z> 1: ──RX(0.20)───────RZ(1.00)─┤ 2: ──H─────────┤↗├──║────────┤ ╚═══╝