paulicirc.builders

Circuit builders

CircuitBuilder

class CircuitBuilder(num_qubits, *, init_capacity=None, capacity_scaling=2)[source]

Bases: object

A utility class to build circuits using common gate-based syntax.

static __new__(cls, num_qubits, *, init_capacity=None, capacity_scaling=2)[source]

Create an empty circuit builder with the given number of qubits.

Optionally, initial capacity and/or capacity scaling can be specified for the underlying circuit. By default, the initial capacity is set to the number of qubits (with a minimum of 1), while the capacity scaling factor is set to 2 (doubling capacity whenever required).

Parameters:
Return type:

Self

append(gadget)[source]

Appends a single gadget to the circuit being built.

Parameters:

gadget (Gadget)

Return type:

None

property capacity

The current capacity of the circuit builder.

Return type:

int

ccx(c0, c1, t)[source]

Adds a CCX gate to the given control and target qubits.

Parameters:
Return type:

None

ccy(c0, c1, t)[source]

Adds a CCY gate to the given control and target qubits.

Parameters:
Return type:

None

ccz(c0, c1, t)[source]

Adds a CCZ gate to the given control and target qubits.

Parameters:
Return type:

None

circuit()[source]

Returns a circuit constructed from the gadgets currently in the builder.

Return type:

Circuit

cswap(c, t0, t1)[source]

Adds a CSWAP gate to the given control and target qubits.

Parameters:
Return type:

None

cx(c, t)[source]

Adds a CX gate to the given control and target qubits.

Parameters:
Return type:

None

cy(c, t)[source]

Adds a CY gate to the given control and target qubits.

Parameters:
Return type:

None

cz(c, t)[source]

Adds a CZ gate to the given control and target qubits.

Parameters:
Return type:

None

extend(gadgets)[source]

Appends the given gadgets to the circuit being built. Gadgets are all validated prior to any modification, so either they are all appended to the circuit or none is.

Parameters:

gadgets (Sequence[Gadget] | Circuit)

Return type:

None

h(q, *, xzx=False)[source]

Adds a H gate on the given qubit.

By default, this is decomposed as Z(pi/2)X(pi/2)Z(pi/2), but setting xzx=True decomposes it as X(pi/2)Z(pi/2)X(pi/2) instead.

Parameters:
Return type:

None

hdg(q, *, xzx=False)[source]

Adds a H gate on the given qubit.

By default, this is decomposed as Z(-pi/2)X(-pi/2)Z(-pi/2), but setting xzx=True decomposes it as X(-pi/2)Z(-pi/2)X(-pi/2) instead.

Parameters:
Return type:

None

property num_qubits

Number of qubits for the circuit.

Return type:

int

rx(phase, q)[source]

Adds an X rotation with given angle on the given qubit(s).

Parameters:
Return type:

None

ry(phase, q)[source]

Adds a Y rotation with given angle on the given qubit(s).

Parameters:
Return type:

None

rz(phase, q)[source]

Adds a Z rotation with given angle on the given qubit(s).

Parameters:
Return type:

None

s(q)[source]

Adds a S gate on the given qubit.

Parameters:

q (QubitIdx | QubitIdxs)

Return type:

None

sdg(q)[source]

Adds a S† gate on the given qubit.

Parameters:

q (QubitIdx | QubitIdxs)

Return type:

None

set_capacity(new_capacity)[source]

Sets the circuit capacity to the given value.

Parameters:

new_capacity (int)

Return type:

None

statevec(input, _normalise_phase=False)[source]

Computes the statevector resulting from the application of the circuit being built to the given input statevector.

Parameters:
Return type:

Complex128Array1D

swap(c, t)[source]

Adds a SWAP gate to the given control and target qubits.

Parameters:
Return type:

None

sx(q)[source]

Adds a √X gate on the given qubit.

Parameters:

q (QubitIdx | QubitIdxs)

Return type:

None

sxdg(q)[source]

Adds a √X† gate on the given qubit.

Parameters:

q (QubitIdx | QubitIdxs)

Return type:

None

t(q)[source]

Adds a T gate on the given qubit.

Parameters:

q (QubitIdx | QubitIdxs)

Return type:

None

tdg(q)[source]

Adds a T† gate on the given qubit.

Parameters:

q (QubitIdx | QubitIdxs)

Return type:

None

trim_capacity()[source]

Sets the circuit capacity to the minimum amount possible.

Return type:

None

unitary(*, _normalise_phase=True)[source]

Returns the unitary matrix associated to the circuit being built.

Parameters:

_normalise_phase (bool; default = True)

Return type:

Complex128Array2D

x(q)[source]

Adds a X gate on the given qubit.

Parameters:

q (QubitIdx | QubitIdxs)

Return type:

None

y(q)[source]

Adds a Y gate on the given qubit.

Parameters:

q (QubitIdx | QubitIdxs)

Return type:

None

z(q)[source]

Adds a Z gate on the given qubit.

Parameters:

q (QubitIdx | QubitIdxs)

Return type:

None