Circuit machinery

Circuit machinery#

Turns a Yao-style gate list into a program and applies it to an image one gate at a time; every basis with the same program shares one compiled applier. Every basis uses it.

Circuit machinery shared by every basis: gates, programs and the applier.

A family emits a Yao-style gate list; compile_program turns it into a Program and the tensors in stored order (Hadamard-first, Yao little-endian qubits); CircuitCode applies the program one gate at a time.

Contents

Program

The structure of a circuit: which gate acts on which qubits, in what order.

compile_program

The program of a gate sequence on m + n qubits, and its tensors in stored order.

CircuitCode

code(*tensors, pic): a program applied to pic of shape (2,) * (m + n).

compile_circuit

The applier of a gate sequence and its initial tensors.

apply_program

Apply a program to an image, or to a stack of images.

apply_circuit

Apply code to one (2**m, 2**n) image, in double precision.

contract_circuit

Run code on pic laid out one axis per qubit, and give back a (2**m, 2**n) array.

check_image_shape

Refuse an image that is not (2**m, 2**n).

bit_reverse

Pi x Pi: x with the bits of its row index and of its column index reversed.

register_width

The number of qubits of a register that holds size values; size must be a power of two.

Gate

One gate of a circuit program.

GATE_KINDS

Built-in immutable sequence.

REGISTERS

Built-in immutable sequence.

HADAMARD

hadamard_gate

A Hadamard on qubit q.

phase_gate

The constructor gate(q_ctrl, q_tgt, phi) of a controlled phase in one of its two forms.

cp_gate

u4_gate

two_registers

A separable 2-D circuit: emit_1d(n_qubits, offset) on the row qubits, then on the column qubits.

hadamards_then_layers

A Hadamard on every qubit, then one layer of phase gates on each register.

check_qubits

Every 2-D circuit needs at least one qubit per register.

phase_list

count angles as floats: zeros when phases is None, else phases checked for length.

controlled_phase_diag

2x2 compact representation of a 4x4 controlled-phase gate.

u4_from_phase

Return the 2-qubit (2, 2, 2, 2)-shaped tensor matching the 4×4 controlled- phase gate diag(1, 1, 1, exp(iφ)).

controlled

The dense two-qubit tensor that applies the one-qubit block where the control is 1.

identity_tensor

The tensor with which a gate of kind does nothing, as complex128.

extract_phase_from_cp

Extract φ from a compact 2x2 CP tensor [[1, 1], [1, e^iφ]].

extract_phases

The phase of each compact CP tensor at gate_indices, in that order.

is_compact_cp

True if tensor looks like a compact 2x2 CP gate [[1, 1], [1, e^iφ]].

select_last_n_cp_indices

Return indices of the LAST n_gates compact-CP tensors in tensors.