Skip to content

Add an example to the to_openqasm docs to show how to use the wires kwarg to avoid qubit reordering #9768

Description

@gsbgithub

Expected behavior

The generated OpenQASM should preserve the wire ordering and gate semantics of the original PennyLane circuit.

Actual behavior

The exported OpenQASM remaps gate operands

Additional information

The OpenQASM generated by qml.to_openqasm() does not match the circuit definition for a simple 3-qubit circuit containing Pauli-X and Toffoli gates.

The circuit is defined as:

  • Apply X on wire 0
  • Apply X on wire 2
  • Apply Toffoli with controls (0,1) and target (2)

However, the exported OpenQASM applies X on wire 1 instead of wire 2 and exports the Toffoli as ccx q[0],q[2],q[1],
which changes the circuit semantics.

Source code

import pennylane as qml
n_qubits = 3
dev = qml.device("default.qubit", wires=n_qubits)
@qml.qnode(dev)
def penny_qc():
  qml.X(0)
  qml.X(2)
  # controls = 0,1 ; target = 2
  qml.Toffoli(wires=[0, 1, 2])
  return qml.state()

# circuit structure
tape = qml.workflow.construct_tape(penny_qc)()
print("Operations:")
for op in tape.operations:
  print(op.name, list(op.wires))

print("\nCircuit:")
print(qml.draw(penny_qc)())

print("\nOpenQASM:")
qasm_str = qml.to_openqasm(penny_qc)()
print(qasm_str)


## Output:
## Operations


PauliX [0]
PauliX [2]
Toffoli [0, 1, 2]


## Circuit

0: ──X─╭●─┤
1: ────├●─┤
2: ──X─╰X─┤


## Generated OpenQASM


OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c[3];

x q[0];
x q[1];
ccx q[0],q[2],q[1];

measure q[0] -> c[0];
measure q[1] -> c[1];
measure q[2] -> c[2];


## Expected OpenQASM


OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c[3];

x q[0];
x q[2];
ccx q[0],q[1],q[2];

measure q[0] -> c[0];
measure q[1] -> c[1];
measure q[2] -> c[2];

Tracebacks

System information

Name: pennylane
Version: 0.45.1
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: 
Author: 
License: 
Location: F:\anaconda3\envs\qiskitenv\Lib\site-packages
Platform info:           Windows-10-10.0.19045-SP0
Python version:          3.11.5
Numpy version:           2.4.0
Scipy version:           1.16.3
JAX version:             None
Catalyst version:        None
Installed devices:
- default.clifford (pennylane-0.45.1)
- default.gaussian (pennylane-0.45.1)
- default.mixed (pennylane-0.45.1)
- default.qubit (pennylane-0.45.1)
- default.qutrit (pennylane-0.45.1)
- default.qutrit.mixed (pennylane-0.45.1)
- default.tensor (pennylane-0.45.1)
- null.qubit (pennylane-0.45.1)
- reference.qubit (pennylane-0.45.1)
- lightning.qubit (pennylane_lightning-0.45.0)
- qiskit.aer (PennyLane-qiskit-0.45.0)
- qiskit.basicaer (PennyLane-qiskit-0.45.0)
- qiskit.basicsim (PennyLane-qiskit-0.45.0)
- qiskit.remote (PennyLane-qiskit-0.45.0)

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions