Skip to content

QuantumCircuit

QuantumCircuit

The abstract QuantumCircuit is the key object in Qadence, as it is what can be executed.

QuantumCircuit(support, *blocks) dataclass

Am abstract QuantumCircuit instance.

It needs to be passed to a quantum backend for execution.

Arguments:

support: `Register` or number of qubits. If an integer is provided, a register is
    constructed with `Register.all_to_all(x)`
*blocks: (Possibly multiple) blocks to construct the circuit from.
Source code in qadence/circuit.py
def __init__(self, support: int | Register, *blocks: AbstractBlock):
    """
    Arguments:

        support: `Register` or number of qubits. If an integer is provided, a register is
            constructed with `Register.all_to_all(x)`
        *blocks: (Possibly multiple) blocks to construct the circuit from.
    """
    self.block = chain(*blocks) if len(blocks) != 1 else blocks[0]
    self.register = Register(support) if isinstance(support, int) else support

    global_block = isinstance(self.block, AnalogBlock) and self.block.qubit_support.is_global
    if not global_block and len(self.block) and self.block.n_qubits > self.register.n_qubits:
        raise ValueError(
            f"Register with {self.register.n_qubits} qubits is too small for the "
            f"given block with {self.block.n_qubits} qubits"
        )

unique_parameters: list[Parameter] property

Return the unique parameters in the circuit.

These parameters are the actual user-facing parameters which can be assigned by the user. Multiple gates can contain the same unique parameter

RETURNS DESCRIPTION
list[Parameter]

list[Parameter]: List of unique parameters in the circuit

dagger()

Reverse the QuantumCircuit by calling dagger on the block.

Source code in qadence/circuit.py
def dagger(self) -> QuantumCircuit:
    """Reverse the QuantumCircuit by calling dagger on the block."""
    return QuantumCircuit(self.n_qubits, self.block.dagger())

get_blocks_by_tag(tag)

Extract one or more blocks using the human-readable tag.

This function recursively explores all composite blocks to find all the occurrences of a certain tag in the blocks.

PARAMETER DESCRIPTION
tag

the tag to look for

TYPE: str

RETURNS DESCRIPTION
list[AbstractBlock]

list[AbstractBlock]: The block(s) corresponding to the given tag

Source code in qadence/circuit.py
def get_blocks_by_tag(self, tag: str) -> list[AbstractBlock]:
    """Extract one or more blocks using the human-readable tag.

    This function recursively explores all composite blocks to find
    all the occurrences of a certain tag in the blocks.

    Args:
        tag (str): the tag to look for

    Returns:
        list[AbstractBlock]: The block(s) corresponding to the given tag
    """

    def _get_block(block: AbstractBlock) -> list[AbstractBlock]:
        blocks = []
        if block.tag == tag:
            blocks += [block]
        if isinstance(block, CompositeBlock):
            blocks += flatten(*[_get_block(b) for b in block.blocks])
        return blocks

    return _get_block(self.block)

parameters()

Extract all parameters for primitive blocks in the circuit.

Notice that this function returns all the unique Parameters used in the quantum circuit. These can correspond to constants too.

RETURNS DESCRIPTION
list[Parameter | Basic] | list[tuple[Parameter | Basic, ...]]

List[tuple[Parameter]]: A list of tuples containing the Parameter

list[Parameter | Basic] | list[tuple[Parameter | Basic, ...]]

instance of each of the primitive blocks in the circuit or, if the flatten

list[Parameter | Basic] | list[tuple[Parameter | Basic, ...]]

flag is set to True, a flattened list of all circuit parameters

Source code in qadence/circuit.py
def parameters(self) -> list[Parameter | Basic] | list[tuple[Parameter | Basic, ...]]:
    """Extract all parameters for primitive blocks in the circuit.

    Notice that this function returns all the unique Parameters used
    in the quantum circuit. These can correspond to constants too.

    Returns:
        List[tuple[Parameter]]: A list of tuples containing the Parameter
        instance of each of the primitive blocks in the circuit or, if the `flatten`
        flag is set to True, a flattened list of all circuit parameters
    """
    return parameters(self.block)