pdhoolia / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
0 stars 0 forks source link

`qiskit.transpile` with `optimization_level=3` does not optimize away an identity operation like `cp` with phase 0 #1

Open pdhoolia opened 1 month ago

pdhoolia commented 1 month ago

What is happening?

Why does qiskit.transpile() function with optimization level 3 not remove a cp (controlled phase) gate when its parameter is 0? Since a cp(0) gate is equivalent to an identity operation, it should be optimized away.

How can we reproduce the issue?

import qiskit
import qiskit_aer
circuit = qiskit.QuantumCircuit(2)
circuit.cp(0, 1, 0)
circuit = qiskit.transpile(circuit, qiskit_aer.AerSimulator(), optimization_level=3)
circuit.data

Output:

[CircuitInstruction(operation=Instruction(name='cp', num_qubits=2, num_clbits=0, params=[0.0]), qubits=(Qubit(QuantumRegister(2, 'q'), 1), Qubit(QuantumRegister(2, 'q'), 0)), clbits=())]

What should happen?

The gate should be removed.

se-agent commented 1 month ago

To address the issue of the cp(0) gate not being removed during optimization, we need to ensure that the optimization passes in Qiskit are capable of recognizing and removing identity operations, such as a controlled phase gate with a zero phase. The cp(0) gate is equivalent to an identity operation and should be optimized away.

We can address this by modifying or adding logic in the optimization passes to recognize and remove such identity operations. Here's a suggested approach:

  1. Modify the Optimize1qGates pass: Since cp is a two-qubit gate, we need to handle it in a relevant optimization pass that deals with multi-qubit gates. Currently, Optimize1qGates focuses on single-qubit gates, so we need to ensure that multi-qubit identity gates are also optimized away.

  2. Add logic in the CommutativeCancellation pass: This pass can be extended to recognize and remove cp(0) gates as they are equivalent to identity operations.

Here's a possible modification to the CommutativeCancellation pass to include this logic:

# In file: qiskit.transpiler.passes.optimization.commutative_cancellation.py

from qiskit.circuit.library.standard_gates.cp import CPhaseGate

class CommutativeCancellation(TransformationPass):
    """Cancel the redundant (self-adjoint) gates through commutation relations, including identity gates."""

    def run(self, dag):
        """Run the CommutativeCancellation pass on `dag`.

        Args:
            dag (DAGCircuit): the DAG to be optimized.

        Returns:
            DAGCircuit: the optimized DAG.
        """
        for node in dag.op_nodes():
            # Check for identity operations
            if isinstance(node.op, CPhaseGate) and node.op.params[0] == 0:
                # Remove the cp(0) gate as it is an identity operation
                dag.remove_op_node(node)

        # Existing commutation cancellation logic
        commutation_cancellation.cancel_commutations(dag, self._commutation_checker, self.basis)
        return dag

Reasoning:

This change should be added to the CommutativeCancellation pass or a similar pass that deals with multi-qubit gates. This ensures that identity operations like cp(0) are recognized and removed during the optimization process.