Skip to content

Cannot convert condition in circuit with multiple classical registers to instruction #6584

@chriseclectic

Description

@chriseclectic

Information

  • Qiskit Terra version:
  • Python version:
  • Operating system:

What is the current behavior?

Appending a circuit with multiple conditional registers to an instruction raises an exception (from to_instruction method), however using compose instead of append seems to work.

A related issue is that the "working" circuit from compose assembles to incorrect qobj (see issue #6583)

Steps to reproduce the problem

Take basic teleport circuit as example

def teleport_circuit():
    """Teleport qubit_in to qubit_out"""
    # It would be nice to be able to do this without using
    # register objects

    # Teleport qubit 0 -> 2
    qr = QuantumRegister(3)
    c0 = ClassicalRegister(1)
    c1 = ClassicalRegister(1)
    teleport = QuantumCircuit(qr, c0, c1)
    teleport.h(qr[1])
    teleport.cx(qr[1], qr[2])
    teleport.cx(qr[0], qr[1])
    teleport.h(qr[0])
    teleport.measure(qr[0], c0[0])
    teleport.measure(qr[1], c1[0])
    teleport.z(qr[2]).c_if(c0, 1)
    teleport.x(qr[2]).c_if(c1, 1)

Appending this to another circuit with

qc = QuantumCircuit(3, 2)
qc.append(teleport_circuit(), range(3), range(2))

raises

~/git/qiskit/qiskit-terra/qiskit/converters/circuit_to_instruction.py in circuit_to_instruction(circuit, parameter_map, equivalence_library)
    116             else:
    117                 raise QiskitError(
--> 118                     "Cannot convert condition in circuit with "
    119                     "multiple classical registers to instruction"
    120                 )

QiskitError: 'Cannot convert condition in circuit with multiple classical registers to instruction'

This error seems to come directly from teleport_circuit().to_instruction() which gets called by append.

If you do compose it produces a circuit, however the conditional values appear incorrect on the drawer and in the assembled qobj (see #6583)

qc = QuantumCircuit(3, 3)
qc = qc.compose(teleport_circuit(), range(3), range(2))
print(qc.draw())
                    ┌───┐┌─┐              
q_0: ────────────■──┤ H ├┤M├──────────────
     ┌───┐     ┌─┴─┐└┬─┬┘└╥┘              
q_1: ┤ H ├──■──┤ X ├─┤M├──╫───────────────
     └───┘┌─┴─┐└───┘ └╥┘  ║  ┌───┐  ┌───┐ 
q_2: ─────┤ X ├───────╫───╫──┤ Z ├──┤ X ├─
          └───┘       ║   ║  └─╥─┘  └─╥─┘ 
                      ║   ║ ┌──╨──┐┌──╨──┐
c: 3/═════════════════╩═══╩═╡ = 1 ╞╡ = 2 ╞
                      1   0 └─────┘└─────┘

What is the expected behavior?

to_instruction should work for any circuit by flatting registers.

Suggested solutions

The implementation in compose seems to work, so maybe how it is implemented could be used for building the flattened circuit definition in the instruction-ified circuit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions