question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a new QuantumCircuit constructor QuantumCircuit.from_instructions

See original GitHub issue

What should we add?

Creating a QuantumCircuit and appending operations into it is a very common pattern. It would be very convenient to be able to create a QuantumCircuit by directly passing an iterator of operations, something like:

@staticmethod
def from_ops(ops: Iterator[tuple[Instruction, Sequence[Qubit], Sequence[Clbit]]]) -> QuantumCircuit:
    circuit = QuantumCircuit()
    for op in ops:
        circuit.append(*ops)
    return circuit

Currently this is not possible as written but it would be if my proposal https://github.com/Qiskit/qiskit-terra/issues/8707 were implemented.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
jakelishmancommented, Sep 8, 2022

It’s important for it to be a sequence, because it defines the order that bits are applied to it when a circuit is used as an Instruction definition, for example - we associate CircuitInstruction.qubits with the QuantumCircuit.qubits by equal indices in the sequence. We do actually have the set view already, it’s just a private attribute - it’s QuantumCircuit._qubit_indices, which is a dict. (I forgot about that when I was saying we’d need to track the set separately.)

1reaction
jakelishmancommented, Sep 8, 2022

For a community member wanting to have a go at this, the rough interface would look like

class QuantumCircuit
    @classmethod
    def from_instructions(cls, instructions: Iterable[CircuitInstruction], *, name=None, metadata=None, global_phase=None):
        out = cls(name=name, metadata=metadata, global_phase=global_phase)
        # ...

CircuitInstruction always contains only Qubit and Clbit instances, so we’d need to track the bits used in a set, and append them to the circuit before appending the instruction if they’re not already there. We’d also need to take care to ensure that any bits or registers that appear in the condition field of an Instruction instance are added as well. For control-flow instructions, the method would need to inspect the blocks field (which contains QuantumCircuit instances, and ensure any registers in there are also in the top-level circuit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

qiskit.circuit.QuantumCircuit
Create a new circuit. A circuit is a list of instructions bound to some registers. ... The registers to be included in the...
Read more >
How to append an Instruction to a QuantumCircuit with ...
Basically, what I'm trying to do is the following: I have an Oracle Instruction which I want to append to my QuantumCircuit, ...
Read more >
qiskit-terra/quantumcircuit.py at main - GitHub
"""Create a new circuit. A circuit is a list of instructions bound to some registers.
Read more >
Introduction - Quantum Programming Studio
Name pyQuil Cirq Q# Qubits id I I I 1 x X X X 1 y Y Y Y 1
Read more >
Untitled circuit - IBM Quantum
... quantum circuits on quantum hardware or simulators. First time here? Build your first circuit. Get started. Been here before? See what's new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found