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

`Clifford.to_circuit()` returns a circuit with emtpy `qregs` #4

Open pdhoolia opened 3 weeks ago

pdhoolia commented 3 weeks ago

What is happening?

Clifford.to_circuit() returns a QuantumCircuit with empty qregs. I don't know if this is an expected behaviour, but it has changed since qiskit 1.1.2.

How can we reproduce the issue?

from qiskit import QuantumCircuit
from qiskit.quantum_info import Clifford

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
cli = Clifford(qc)
cli.to_circuit().qregs

What should happen?

I would expect the QuantumCircuit to contain one QuantumRegister with the correct size, as it was the case in qiskit 1.1.2

>>> cli.to_circuit().qregs
[QuantumRegister(2, 'q')]
se-agent commented 3 weeks ago

The issue you're experiencing is related to the Clifford.to_circuit() method, which currently returns a QuantumCircuit with empty quantum registers (qregs). This behavior appears to have changed from previous versions of Qiskit.

To address this issue, we need to ensure that when converting a Clifford object back to a QuantumCircuit, the resulting circuit includes the appropriate quantum registers (QuantumRegister) that match the number of qubits in the Clifford object.

Here's how you can modify the Clifford.to_circuit() method to ensure it includes quantum registers:

  1. Modify the to_circuit() method in qiskit.quantum_info.operators.symplectic.clifford.py to create and add a QuantumRegister to the QuantumCircuit before returning it.

Here is the updated code for the to_circuit() method:

def to_circuit(self):
    """Return a QuantumCircuit implementing the Clifford.

    For N <= 3 qubits this is based on optimal CX cost decomposition
    from reference [1]. For N > 3 qubits this is done using the general
    non-optimal compilation routine from reference [2].

    Return:
        QuantumCircuit: a circuit implementation of the Clifford.

    References:
        1. S. Bravyi, D. Maslov, *Hadamard-free circuits expose the
           structure of the Clifford group*,
           `arXiv:2003.09412 [quant-ph] <https://arxiv.org/abs/2003.09412>`_

        2. S. Aaronson, D. Gottesman, *Improved Simulation of Stabilizer Circuits*,
           Phys. Rev. A 70, 052328 (2004).
           `arXiv:quant-ph/0406196 <https://arxiv.org/abs/quant-ph/0406196>`_
    """
    from qiskit.synthesis.clifford import synth_clifford_full  # pylint: disable=cyclic-import

    # Create a QuantumRegister for the Clifford
    qreg = QuantumRegister(self.num_qubits, 'q')

    # Generate the circuit using the synthesis method
    circuit = synth_clifford_full(self)

    # Add the QuantumRegister to the circuit
    circuit.add_register(qreg)

    return circuit

Explanation:

This change ensures that the QuantumCircuit returned by Clifford.to_circuit() includes the appropriate quantum registers, thus restoring the expected behavior as seen in earlier versions of Qiskit.