liveBook · Manning

Custom quantum gate

As explained in Quantum Programming in Depth

A custom quantum gate is a user-defined quantum operation implementing a specified reversible transformation.

A custom quantum gate is a user-defined quantum operation that implements a specified reversible transformation. In state preparation, it packages a sequence of gates so the operation can be reused, controlled, tested, and applied to selected qubits. section 2.3.2 section 2.3.3

A two-qubit state-preparation routine follows this decomposition:

  • Compute b0 = √(a0² + a2²) and b1 = √(a1² + a3²), then prepare the least significant qubit with amplitudes b0 and b1. section 2.3.2
  • Use a controlled preparation gate to transform the |1⟩ branch into a1|10⟩ + a3|11⟩. section 2.3.2
  • Use a controlled-on-zero preparation gate to transform the |0⟩ branch into a0|00⟩ + a2|01⟩. section 2.3.2
Figure 2.4 Preparing a two-qubit state. Each set of four bars represents the amplitudes of the basis states: from left to right, \(\ket{0}\), \(\ket{1}\), \(\ket{2}\), and \(\ket{3}\). Light bars indicate zero amplitudes (basis states not present in the superposition state), dark bars indicate nonzero amplitudes (positive or negative). We prepare the least significant qubit in a superposition state and then adjust the state of the most significant qubit depending on the state of the least significant qubit using controlled gates. The left path handles the case of the least significant qubit being in the \(\ket{0}\) state, and the right path handles the \(\ket{1}\) state. Striped bars represent the amplitudes of the basis states that are involved in each path.
figure

Trace the two controlled paths and their amplitude contributions across the four computational basis states. section 2.3.2

In Qiskit, define a custom gate by constructing a QuantumCircuit, adding its component gates, and converting it with to_gate. Append it with append, or derive a controlled version with control. Pass ctrl_state='0' or 0 for control on |0⟩. Transpile circuits containing multiqubit custom gates before running them on a simulator or device. section 2.3.3

In Q#, implement a gate as an operation on qubits with return type Unit. Mark operations with is Adj + Ctl when adjoint and controlled specializations are available, then invoke them with Adjoint, Controlled, or ApplyControlledOnInt. section 2.3.4

Verifying a prepared state

Applying the two-qubit routine to [0.36, 0.48, 0.64, -0.48] produces amplitudes 0.36|00⟩ + 0.48|01⟩ + 0.64|10⟩ − 0.48|11⟩. Qiskit can save the statevector, while Q# DumpMachine displays amplitudes, probabilities, and phase.

How Quantum Programming in Depth covers it

How deep it goes
Derives a two-qubit decomposition, implements it in two frameworks, and verifies amplitudes while deferring broader gate synthesis details.
Examples
Demonstrates two-qubit preparation using coefficients [0.36, 0.48, 0.64, −0.48] in Qiskit and Q#.
What it assumes
Assumes basic quantum-state notation and single-qubit preparation while refreshing the decomposition needed for a two-qubit state.
What you can do after
Build custom and controlled gates, transpile them, run statevector simulations, and inspect the resulting amplitudes and probabilities.
How it connects
Connects custom gates with quantum state preparation, controlled gates, controlled-on-zero operations, adjoints, and transpilation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Quantum Programming in Depth. The section links point to the parts of the book it draws on.