Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QuantumCircuit .clear() and .copy_empty_like() functionality #8132

Closed
2 tasks
pedrorrivero opened this issue Jun 2, 2022 · 3 comments · Fixed by #8134
Closed
2 tasks

QuantumCircuit .clear() and .copy_empty_like() functionality #8132

pedrorrivero opened this issue Jun 2, 2022 · 3 comments · Fixed by #8134
Labels
type: feature request New feature or request

Comments

@pedrorrivero
Copy link
Member

What should we add?

Sometimes it can be useful to erase all instructions in a QuantumCircuit. This request is for:

  1. Clear functionality to erase all instructions from a QuantumCircuit → .clear().
    We not only need to clear all data, but (at least) also update the parameter table:
self.data.clear()
self._parameter_table.clear()
  1. Copy empty functionality to get a cleared copy of a QuantumCircuit → .copy_empty_like().
    This functionality is already implemented for DAGCircuit, and should retain all relevant circuit properties other than the instructions and associated parameters (e.g. registers, bits, name, metadata). Notice that quantum_circuit.copy().clear() would give the desired outcome, however .copy() performs a deepcopy of the circuit, which we would then erase almost completely when deleting the instructions, so this method should provide a more efficient way to do this.

TODO:

  • .clear()
  • .copy_empty_like()
@pedrorrivero pedrorrivero added the type: feature request New feature or request label Jun 2, 2022
@pedrorrivero
Copy link
Member Author

@mtreinish

@pedrorrivero pedrorrivero changed the title .clear() and .copy_empty_like() functionality in QuantumCircuit QuantumCircuit .clear() and .copy_empty_like() functionality Jun 2, 2022
@Cryoris
Copy link
Contributor

Cryoris commented Jun 3, 2022

Sounds like these could be useful, do you need this for e.g. copying attached calibrations or what's the usecase you have in mind? 🙂

@pedrorrivero
Copy link
Member Author

@Cryoris my particular use case is building new circuits based on an original one. For instance to duplicate instructions:

def build_new_circuit(circuit: QuantumCircuit) -> QuantumCircuit:
    new_circuit = circuit.copy_empty_like()
    for instruction, qargs, cargs in circuit:
        new_circuit.append(instruction, qargs, cargs)
        new_circuit.append(instruction, qargs, cargs)
    return new_circuit

As you can see, it is important to retain the same quantum and classical registers, but also all relevant metadata as you mention. The easiest way forward is to create a cleared copy of the circuit.

I would therefore aim for qc.copy_empty_like() to be logically equivalent to qc.copy().clear(), but more efficient. I think this is the same behavior as in DAGCircuit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants