qml.breakpoint¶
- breakpoint()[source]¶
A function which freezes execution and launches the PennyLane debugger (PLDB).
This function marks a location in a quantum circuit (QNode). When it is encountered during execution of the quantum circuit, an interactive debugging prompt is launched to step through the circuit execution. Since it is based on the Python Debugger (PDB), commands like (
list
,next
,continue
,quit
) can be used to navigate the code.See also
Example
Consider the following python script containing the quantum circuit with breakpoints.
1import pennylane as qml 2 3dev = qml.device("default.qubit", wires=2) 4 5@qml.qnode(dev) 6def circuit(x): 7 qml.breakpoint() 8 9 qml.RX(x, wires=0) 10 qml.Hadamard(wires=1) 11 12 qml.breakpoint() 13 14 qml.CNOT(wires=[0, 1]) 15 return qml.expval(qml.Z(0)) 16 17circuit(1.23)
Running the above python script opens up the interactive
[pldb]
prompt in the terminal. The prompt specifies the path to the script along with the next line to be executed after the breakpoint.> /Users/your/path/to/script.py(9)circuit() -> qml.RX(x, wires=0) [pldb]
We can interact with the prompt using the commands:
list
,next
,continue
, andquit
. Additionally, we can also access any variables defined in the function.[pldb] x 1.23
The
list
command will print a section of code around the breakpoint, highlighting the next line to be executed.[pldb] list 5 @qml.qnode(dev) 6 def circuit(x): 7 qml.breakpoint() 8 9 -> qml.RX(x, wires=0) 10 qml.Hadamard(wires=1) 11 12 qml.breakpoint() 13 14 qml.CNOT(wires=[0, 1]) 15 return qml.expval(qml.Z(0)) [pldb]
The
next
command will execute the next line of code, and print the new line to be executed.[pldb] next > /Users/your/path/to/script.py(10)circuit() -> qml.Hadamard(wires=1) [pldb]
The
continue
command will resume code execution until another breakpoint is reached. It will then print the new line to be executed. Finally,quit
will resume execution of the file and terminate the debugging prompt.[pldb] continue > /Users/your/path/to/script.py(14)circuit() -> qml.CNOT(wires=[0, 1]) [pldb] quit