# This code is part of QSteed.
#
# (C) Copyright 2024 Beijing Academy of Quantum Information Sciences
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from quafu import QuantumCircuit


def reverse_circuit(circuit: QuantumCircuit):
    """
    This function aims to provide a reversed circuit for a given circuit.

    Args:
        circuit: input circuit that need to be reversed
    Return:
        the reversed circuit aimed to do bidirectional routing
    """
    rev_circuit = QuantumCircuit(circuit.num)

    if circuit.measures:
        for qubit, cbit in circuit.measures.items():
            rev_circuit.measure([qubit], [cbit])

    for gate in circuit.gates[::-1]:
        rev_circuit.add_gate(gate)
    return rev_circuit
