Vai al contenuto Inglorious Coderz EN

← Tutti gli articoli

Quantum Computing

Reverse engineering dei circuiti quantistici

Un approccio algoritmico per definire e analizzare circuiti quantistici a più qubit.

· di Matteo Antony Mistretta

Questo articolo è stato scritto originariamente in inglese. Leggi l'originale →

Quando si studiano le basi del quantum computing si comincia a descrivere i circuiti con semplici calcoli su matrici 2×22 \times 2 o 4×44 \times 4, ma appena si esplorano i primi algoritmi ci si imbatte in oracoli che sembrano troppo complessi per essere rappresentati così. Eppure le matrici sono molto importanti per noi sviluppatori: sono come funzioni che possiamo progettare e ispezionare indipendentemente dall’input. Se potessimo descrivere qualunque circuito come una semplice combinazione di matrici, programmare circuiti sarebbe simile a programmare funzioni. Deve esserci un modo. Forse ne ho trovato uno.

Cominciamo importando le librerie necessarie e definendo alcune funzioni di comodo per disegnare facilmente circuiti e matrici unitarie:

from qiskit import QuantumCircuit, execute, Aer
import math

def draw(circuit):
    return circuit.draw(output='mpl')

def unitary(circuit):
    unitary = execute(circuit, Aer.get_backend('unitary_simulator')).result().get_unitary()
    pretty_unitary = ''
    for row in range(len(unitary)):
        pretty_unitary += '|'
        for column in range(len(unitary[row])):
            value = unitary[row][column]
            if math.isclose(value.imag, 0, abs_tol=0.01):
                pretty_value = '{num.real:.0f}'.format(num=value)
            elif math.isclose(value.real, 0, abs_tol=0.01):
                pretty_value = '{num.imag:.0f}i'.format(num=value)
            else:
                pretty_value = '{num.real:.0f}+{num.imag:.0f}i'.format(num=value)

            pretty_unitary += pretty_value
            if column < len(unitary[row])-1:
                pretty_unitary += ' '
        pretty_unitary += '|\n'
    print(pretty_unitary)

Dato che avremo a che fare con matrici enormi, per semplificare i calcoli definiamo anche le seguenti due matrici “atomiche”:

0ˉ=∣0⟩⟨0∣=[1000]1ˉ=∣1⟩⟨1∣=[0001] \begin{gathered} \bar{0} = |0\rangle\langle0| = \begin{bmatrix} 1 & 0\\ 0 & 0 \end{bmatrix} \\[2em] \bar{1} = |1\rangle\langle1| = \begin{bmatrix} 0 & 0\\ 0 & 1 \end{bmatrix} \end{gathered}

Il loro significato è semplicemente “lo stato attuale del qubit è ∣0⟩|0\rangle (o ∣1⟩|1\rangle, rispettivamente).”

Anche se questi atomi non sono reversibili (ad es. 0ˉ⋅0ˉ†=0ˉ≠I\bar{0} \cdot \bar{0}^\dagger = \bar{0} \not = I), permettono di definire facilmente altre matrici come loro combinazione:

I=[1001]=[1000]+[0001]=0ˉ+1ˉCX=[1000000100100100]=[0ˉ1ˉ1ˉ0ˉ]=[0ˉ000ˉ]+[01ˉ1ˉ0]=I⊗0ˉ+X⊗1ˉ \begin{gathered} I = \begin{bmatrix} 1 & 0\\ 0 & 1 \end{bmatrix} = \begin{bmatrix} 1 & 0\\ 0 & 0 \end{bmatrix} + \begin{bmatrix} 0 & 0\\ 0 & 1 \end{bmatrix} = \bar{0} + \bar{1} \\[2em] CX = \begin{bmatrix} 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 1\\ 0 & 0 & 1 & 0\\ 0 & 1 & 0 & 0 \end{bmatrix} = \begin{bmatrix} \bar{0} & \bar{1}\\ \bar{1} & \bar{0} \end{bmatrix} = \begin{bmatrix} \bar{0} & 0\\ 0 & \bar{0} \end{bmatrix} + \begin{bmatrix} 0 & \bar{1}\\ \bar{1} & 0 \end{bmatrix} = I \otimes \bar{0} + X \otimes \bar{1} \end{gathered}

Notate come quest’ultima somma si possa interpretare facilmente così: “Il secondo qubit resta invariato se il primo qubit è ∣0⟩|0\rangle, ma viene invertito se il primo qubit è ∣1⟩|1\rangle.” È esattamente la descrizione di una porta CNOT! Sembra che questa rappresentazione renda i circuiti più intuitivi da descrivere.

Non tutte le matrici possono essere definite con questi due atomi, ad esempio:

X=[0110]=[0010]+[0100]=∣1⟩⟨0∣+∣0⟩⟨1∣ X = \begin{bmatrix} 0 & 1\\ 1 & 0 \end{bmatrix} = \begin{bmatrix} 0 & 0\\ 1 & 0 \end{bmatrix} + \begin{bmatrix} 0 & 1\\ 0 & 0 \end{bmatrix} = |1\rangle\langle0| + |0\rangle\langle1|

Questo suggerisce l’esistenza di un’altra coppia di atomi, ma ai fini dei nostri calcoli possiamo tenere così come sono matrici piccole come II e XX, sapendo che ∣1⟩⟨0∣|1\rangle\langle0| si può definire come X⋅0ˉX \cdot \bar{0} e ∣0⟩⟨1∣|0\rangle\langle1| come X⋅1ˉX \cdot \bar{1}.

Ora siamo in grado di descrivere tutte le porte come combinazione di matrici 2×22 \times 2, il che significa che possiamo:

  1. Fare reverse engineering di enormi matrici unitarie scomponendole in atomi
  2. Descrivere i circuiti definendo ogni porta come prodotto tensoriale di atomi
  3. Fare “forward engineering” dei circuiti, descrivendo il risultato che ci aspettiamo!

CNOT01CNOT_{01}

Sappiamo come funziona la porta CNOT su un circuito a due qubit. Non ci aspettiamo sorprese se aggiungiamo un terzo qubit (del tutto inutile).

circuit = QuantumCircuit(3)
circuit.cx(0, 1)
draw(circuit)

png

La matrice unitaria corrispondente dovrebbe essere piuttosto semplice: basta trattare la porta mancante su q2q_2 come una matrice identità. Trasformiamola in una combinazione di matrici più piccole:

I⊗CX=[CX00CX]=[0ˉ1ˉ001ˉ0ˉ00000ˉ1ˉ001ˉ0ˉ]=[0ˉ00000ˉ00000ˉ00000ˉ]+[01ˉ001ˉ0000001ˉ001ˉ0]=I⊕I⊕0ˉ+I⊕X⊕1ˉ \begin{gathered} I \otimes CX = \begin{bmatrix} CX & 0\\ 0 & CX \end{bmatrix} = \begin{bmatrix} \bar{0} & \bar{1} & 0 & 0\\ \bar{1} & \bar{0} & 0 & 0\\ 0 & 0 & \bar{0} & \bar{1}\\ 0 & 0 & \bar{1} & \bar{0} \end{bmatrix}\\ = \begin{bmatrix} \bar{0} & 0 & 0 & 0\\ 0 & \bar{0} & 0 & 0\\ 0 & 0 & \bar{0} & 0\\ 0 & 0 & 0 & \bar{0} \end{bmatrix} + \begin{bmatrix} 0 & \bar{1} & 0 & 0\\ \bar{1} & 0 & 0 & 0\\ 0 & 0 & 0 & \bar{1}\\ 0 & 0 & \bar{1} & 0 \end{bmatrix}\\ = I \oplus I \oplus \bar{0} + I \oplus X \oplus \bar{1} \end{gathered}

Che possiamo interpretare come: “q1q_1 resta invariato se q0q_0 è uguale a ∣0⟩|0\rangle, ma viene invertito quando è ∣1⟩|1\rangle; q2q_2 è irrilevante.” È esattamente come descrivere una porta CNOT tra q0q_0 e q1q_1, con una matrice identità su q2q_2!

unitary(circuit)
|1 0 0 0 0 0 0 0|
|0 0 0 1 0 0 0 0|
|0 0 1 0 0 0 0 0|
|0 1 0 0 0 0 0 0|
|0 0 0 0 1 0 0 0|
|0 0 0 0 0 0 0 1|
|0 0 0 0 0 0 1 0|
|0 0 0 0 0 1 0 0|

CNOT12\text{CNOT}_{12}

Non cambia molto se il terzo qubit viene invece posizionato in alto, come primo.

circuit = QuantumCircuit(3)
circuit.cx(1, 2)
draw(circuit)

png

Anche qui possiamo calcolare facilmente la matrice unitaria risultante e scomporla in una combinazione di matrici più piccole, proprio come prima:

CX⊗I=[I000000I00I00I00]=[I000000000I00000]+[0000000I00000I00]=I⊗0ˉ⊗I+X⊗1ˉ⊗I \begin{gathered} CX \otimes I = \begin{bmatrix} I & 0 & 0 & 0\\ 0 & 0 & 0 & I\\ 0 & 0 & I & 0\\ 0 & I & 0 & 0 \end{bmatrix}\\ = \begin{bmatrix} I & 0 & 0 & 0\\ 0 & 0 & 0 & 0\\ 0 & 0 & I & 0\\ 0 & 0 & 0 & 0 \end{bmatrix} + \begin{bmatrix} 0 & 0 & 0 & 0\\ 0 & 0 & 0 & I\\ 0 & 0 & 0 & 0\\ 0 & I & 0 & 0 \end{bmatrix}\\ = I \otimes \bar{0} \otimes I + X \otimes \bar{1} \otimes I \end{gathered}

L’interpretazione è molto simile alla precedente, cambia solo l’ordine: “q2q_2 resta invariato se q1q_1 è uguale a ∣0⟩|0\rangle, ma viene invertito se q1q_1 è ∣1⟩|1\rangle; q0q_0 è irrilevante.”

unitary(circuit)
|1 0 0 0 0 0 0 0|
|0 1 0 0 0 0 0 0|
|0 0 0 0 0 0 1 0|
|0 0 0 0 0 0 0 1|
|0 0 0 0 1 0 0 0|
|0 0 0 0 0 1 0 0|
|0 0 1 0 0 0 0 0|
|0 0 0 1 0 0 0 0|

CNOT02\text{CNOT}_{02}

circuit = QuantumCircuit(3)
circuit.cx(0, 2)
draw(circuit)

png

Qui la cosa si complica, perché la II si trova tra il controllo e il target della CNOT. Non ho idea di come costruire questa matrice, quindi lascio il calcolo a Qiskit:

unitary(circuit)
|1 0 0 0 0 0 0 0|
|0 0 0 0 0 1 0 0|
|0 0 1 0 0 0 0 0|
|0 0 0 0 0 0 0 1|
|0 0 0 0 1 0 0 0|
|0 1 0 0 0 0 0 0|
|0 0 0 0 0 0 1 0|
|0 0 0 1 0 0 0 0|

Possiamo vedere questa matrice come:

[0ˉ01ˉ000ˉ01ˉ1ˉ00ˉ001ˉ00ˉ]=[0ˉ00000ˉ00000ˉ00000ˉ]+[001ˉ00001ˉ1ˉ00001ˉ00]=I⊗I⊗0ˉ+X⊗I⊗1ˉ \begin{gathered} \begin{bmatrix} \bar{0} & 0 & \bar{1} & 0\\ 0 & \bar{0} & 0 & \bar{1}\\ \bar{1} & 0 & \bar{0} & 0\\ 0 & \bar{1} & 0 & \bar{0} \end{bmatrix}\\ = \begin{bmatrix} \bar{0} & 0 & 0 & 0\\ 0 & \bar{0} & 0 & 0\\ 0 & 0 & \bar{0} & 0\\ 0 & 0 & 0 & \bar{0} \end{bmatrix} + \begin{bmatrix} 0 & 0 & \bar{1} & 0\\ 0 & 0 & 0 & \bar{1}\\ \bar{1} & 0 & 0 & 0\\ 0 & \bar{1} & 0 & 0 \end{bmatrix}\\ = I \otimes I \otimes \bar{0} + X \otimes I \otimes \bar{1} \end{gathered}

Che potrebbe essere interpretata come: “q2q_2 resta invariato se q0q_0 è uguale a ∣0⟩|0\rangle, ma viene invertito quando q0q_0 è ∣1⟩|1\rangle; q1q_1 è irrilevante.” Esattamente come le due porte precedenti, ma ancora una volta in un ordine diverso!

Ora che sappiamo come fare reverse engineering di una matrice unitaria per ricavarne un circuito, è possibile anche fare il contrario, descrivendo il circuito attraverso le porte che lo compongono, o addirittura fare intuitivamente “forward engineering” di un circuito per ricavarne la matrice? Proviamo con gli oracoli che si incontrano in casi reali.

Teletrasporto quantistico: CNOT12⋅CNOT01\text{CNOT}_{12} \cdot \text{CNOT}_{01}

circuit = QuantumCircuit(3)
circuit.cx(1, 2)
circuit.cx(0, 1)
draw(circuit)

png

Descriviamo questo circuito porta per porta:

(I⊗I⊗0ˉ+I⊗X⊗1ˉ)⋅(I⊗0ˉ⊗I+X⊗1ˉ⊗I) (I \otimes I \otimes \bar{0} + I \otimes X \otimes \bar{1}) \cdot (I \otimes \bar{0} \otimes I + X \otimes \bar{1} \otimes I)

Eseguendo la moltiplicazione otteniamo questo risultato:

=I⊗0ˉ⊗0ˉ+X⊗1ˉ⊗0ˉ+I⊗X⋅0ˉ⊗1ˉ+X⊗X⋅1ˉ⊗1ˉ = I \otimes \bar{0} \otimes \bar{0} + X \otimes \bar{1} \otimes \bar{0} + I \otimes X \cdot \bar{0} \otimes \bar{1} + X \otimes X \cdot \bar{1} \otimes \bar{1}

Un’interpretazione potrebbe essere: “se q0q_0 è ∣0⟩|0\rangle allora q2q_2 dipende solo da q1q_1; se q0q_0 è ∣1⟩|1\rangle allora q1q_1 viene invertito e q2q_2 lo segue a seconda del valore di q1q_1.”

Notate che aggiungere porte di Hadamard non cambia molto il risultato:

circuit = QuantumCircuit(3)
circuit.h(1)
circuit.cx(1, 2)
circuit.cx(0, 1)
circuit.h(0)
draw(circuit)

png

(I⊗I⊗H)⋅(I⊗I⊗0ˉ+I⊗X⊗1ˉ)⋅(I⊗0ˉ⊗I+X⊗1ˉ⊗I)⋅(I⊗H⊗I) \begin{gathered} (I \otimes I \otimes H) \cdot\\ (I \otimes I \otimes \bar{0} + I \otimes X \otimes \bar{1}) \cdot (I \otimes \bar{0} \otimes I + X \otimes \bar{1} \otimes I) \cdot\\ (I \otimes H \otimes I) \end{gathered}
=I⊗0ˉ⋅H⊗H⋅0ˉ+X⊗1ˉ⋅H⊗H⋅0ˉ+I⊗X⋅0ˉ⋅H⊗H⋅1ˉ+X⊗X⋅1ˉ⋅H⊗H⋅1ˉ = I \otimes \bar{0} \cdot H \otimes H \cdot \bar{0} + X \otimes \bar{1} \cdot H \otimes H \cdot \bar{0} + I \otimes X \cdot \bar{0} \cdot H \otimes H \cdot \bar{1} + X \otimes X \cdot \bar{1} \cdot H \otimes H \cdot \bar{1}

Deutsch-Josza: CNOT02⋅CNOT12\text{CNOT}_{02} \cdot \text{CNOT}_{12}

circuit = QuantumCircuit(3)
circuit.cx(0, 2)
circuit.cx(1, 2)
draw(circuit)

png

Che cosa significa questo circuito? Come funziona? Facciamone il reverse engineering guardando la matrice unitaria calcolata:

unitary(circuit)
|1 0 0 0 0 0 0 0|
|0 0 0 0 0 1 0 0|
|0 0 0 0 0 0 1 0|
|0 0 0 1 0 0 0 0|
|0 0 0 0 1 0 0 0|
|0 1 0 0 0 0 0 0|
|0 0 1 0 0 0 0 0|
|0 0 0 0 0 0 0 1|

La matrice ha questo aspetto:

[0ˉ01ˉ001ˉ00ˉ1ˉ00ˉ000ˉ01ˉ]=[0ˉ0000000ˉ000ˉ000ˉ00]+[001ˉ001ˉ001ˉ0000001ˉ]=CX⊗0ˉ+X⊗I⊗I⋅CX⊗1ˉ \begin{gathered} \begin{bmatrix} \bar{0} & 0 & \bar{1} & 0\\ 0 & \bar{1} & 0 & \bar{0}\\ \bar{1} & 0 & \bar{0} & 0\\ 0 & \bar{0} & 0 & \bar{1} \end{bmatrix} = \begin{bmatrix} \bar{0} & 0 & 0 & 0\\ 0 & 0 & 0 & \bar{0}\\ 0 & 0 & \bar{0} & 0\\ 0 & \bar{0} & 0 & 0 \end{bmatrix} + \begin{bmatrix} 0 & 0 & \bar{1} & 0\\ 0 & \bar{1} & 0 & 0\\ \bar{1} & 0 & 0 & 0\\ 0 & 0 & 0 & \bar{1} \end{bmatrix}\\ = CX \otimes \bar{0} + X \otimes I \otimes I \cdot CX \otimes \bar{1} \end{gathered}

Andiamo un po’ più a fondo espandendo le porte CNOT:

=(I⊗0ˉ⊗0ˉ+X⊗1ˉ⊗0ˉ)+(X⊗I⊗I)⋅(I⊗0ˉ⊗1ˉ+X⊗1ˉ⊗1ˉ) = (I \otimes \bar{0} \otimes \bar{0} + X \otimes \bar{1} \otimes \bar{0}) + (X \otimes I \otimes I) \cdot (I \otimes \bar{0} \otimes \bar{1} + X \otimes \bar{1} \otimes \bar{1})

E ora, applicando il prodotto:

=I⊗0ˉ⊗0ˉ+X⊗1ˉ⊗0ˉ+X⊗0ˉ⊗1ˉ+I⊗1ˉ⊗1ˉ = I \otimes \bar{0} \otimes \bar{0} + X \otimes \bar{1} \otimes \bar{0} + X \otimes \bar{0} \otimes \bar{1} + I \otimes \bar{1} \otimes \bar{1}

Che potrebbe essere interpretato come: “q2q_2 viene invertito se solo uno degli altri due è uguale a ∣1⟩|1\rangle. In ogni altro caso resta invariato.” Sembra uno XOR che mantiene gli input in q0q_0 e q1q_1 e conserva l’output in q2q_2. Ora ha senso!

Notate che il risultato trovato suggerisce di scomporre la matrice nel modo seguente, che è equivalente:

[0ˉ0000000000ˉ00000]+[00000000ˉ000000ˉ00]+[001ˉ000001ˉ0000000]+[000001ˉ0000000001ˉ]=[0ˉ01ˉ001ˉ00ˉ1ˉ00ˉ000ˉ01ˉ] \begin{bmatrix} \bar{0} & 0 & 0 & 0\\ 0 & 0 & 0 & 0\\ 0 & 0 & \bar{0} & 0\\ 0 & 0 & 0 & 0 \end{bmatrix} + \begin{bmatrix} 0 & 0 & 0 & 0\\ 0 & 0 & 0 & \bar{0}\\ 0 & 0 & 0 & 0\\ 0 & \bar{0} & 0 & 0 \end{bmatrix} + \begin{bmatrix} 0 & 0 & \bar{1} & 0\\ 0 & 0 & 0 & 0\\ \bar{1} & 0 & 0 & 0\\ 0 & 0 & 0 & 0 \end{bmatrix} + \begin{bmatrix} 0 & 0 & 0 & 0\\ 0 & \bar{1} & 0 & 0\\ 0 & 0 & 0 & 0\\ 0 & 0 & 0 & \bar{1} \end{bmatrix} = \begin{bmatrix} \bar{0} & 0 & \bar{1} & 0\\ 0 & \bar{1} & 0 & \bar{0}\\ \bar{1} & 0 & \bar{0} & 0\\ 0 & \bar{0} & 0 & \bar{1} \end{bmatrix}

Potremmo anche descrivere il circuito come la sequenza delle sue porte:

(I⊗0ˉ⊗I+X⊗1ˉ⊗I)⋅(I⊗I⊗0ˉ+X⊗I⊗1ˉ) (I \otimes \bar{0} \otimes I + X \otimes \bar{1} \otimes I) \cdot (I \otimes I \otimes \bar{0} + X \otimes I \otimes \bar{1})

Ora eseguiamo la moltiplicazione:

=I⊗0ˉ⊗0ˉ+X⊗0ˉ⊗1ˉ+X⊗1ˉ⊗0ˉ+I⊗1ˉ⊗1ˉ = I \otimes \bar{0} \otimes \bar{0} + X \otimes \bar{0} \otimes \bar{1} + X \otimes \bar{1} \otimes \bar{0} + I \otimes \bar{1} \otimes \bar{1}

Che è esattamente il risultato ottenuto con il reverse engineering.

Simon: CNOT02⋅CNOT03⋅CNOT12⋅CNOT13CNOT_{02} \cdot CNOT_{03} \cdot CNOT_{12} \cdot CNOT_{13}

circuit = QuantumCircuit(4)
circuit.cx(0, 2)
circuit.cx(0, 3)
circuit.cx(1, 2)
circuit.cx(1, 3)
draw(circuit)

png

Proviamo un altro “forward engineering”. Il circuito potrebbe essere descritto così:

  1. “q2q_2 viene invertito se q0q_0 o q1q_1 è ∣1⟩|1\rangle”
  2. “q3q_3 viene invertito se q0q_0 o q1q_1 è ∣1⟩|1\rangle”
  3. “Ogni altro caso non cambia lo stato di q2q_2 o q3q_3”

Ora, in matematica:

1. (I⊗X⊗0ˉ⊗1ˉ)+(I⊗X⊗1ˉ⊗0ˉ)2. (X⊗I⊗0ˉ⊗1ˉ)+(X⊗I⊗1ˉ⊗0ˉ)3. (I⊗I⊗0ˉ⊗0ˉ)+(I⊗I⊗1ˉ⊗1ˉ) \begin{gathered} \text{1. } (I \otimes X \otimes \bar{0} \otimes \bar{1}) + (I \otimes X \otimes \bar{1} \otimes \bar{0})\\ \text{2. } (X \otimes I \otimes \bar{0} \otimes \bar{1}) + (X \otimes I \otimes \bar{1} \otimes \bar{0})\\ \text{3. } (I \otimes I \otimes \bar{0} \otimes \bar{0}) + (I \otimes I \otimes \bar{1} \otimes \bar{1}) \end{gathered}

Possiamo semplificare ancora combinando 1. e 2., dato che entrambi i qubit target vengono invertiti contemporaneamente:

1+2. (X⊗X⊗0ˉ⊗1ˉ)+(X⊗X⊗1ˉ⊗0ˉ)3. (I⊗I⊗0ˉ⊗0ˉ)+(I⊗I⊗1ˉ⊗1ˉ) \begin{gathered} \text{1+2. } (X \otimes X \otimes \bar{0} \otimes \bar{1}) + (X \otimes X \otimes \bar{1} \otimes \bar{0})\\ \text{3. } (I \otimes I \otimes \bar{0} \otimes \bar{0}) + (I \otimes I \otimes \bar{1} \otimes \bar{1}) \end{gathered}

Se la descrizione è corretta, la matrice risultante dovrebbe essere:

[0000001ˉ000000000ˉ00001ˉ000000000ˉ00001ˉ000000000ˉ00001ˉ000000000ˉ000000]+[0ˉ000000001ˉ000000000ˉ000000001ˉ000000000ˉ000000001ˉ000000000ˉ000000001ˉ]=[0ˉ000001ˉ001ˉ000000ˉ000ˉ01ˉ0000001ˉ00ˉ00001ˉ00ˉ0000000ˉ01ˉ001ˉ000000ˉ000ˉ000001ˉ] \begin{bmatrix} 0 & 0 & 0 & 0 & 0 & 0 & \bar{1} & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & \bar{0}\\ 0 & 0 & 0 & 0 & \bar{1} & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & \bar{0} & 0 & 0\\ 0 & 0 & \bar{1} & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & \bar{0} & 0 & 0 & 0 & 0\\ \bar{1} & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & \bar{0} & 0 & 0 & 0 & 0 & 0 & 0 \end{bmatrix} + \begin{bmatrix} \bar{0} & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & \bar{1} & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & \bar{0} & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & \bar{1} & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & \bar{0} & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & \bar{1} & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & \bar{0} & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & \bar{1} \end{bmatrix} = \begin{bmatrix} \bar{0} & 0 & 0 & 0 & 0 & 0 & \bar{1} & 0\\ 0 & \bar{1} & 0 & 0 & 0 & 0 & 0 & \bar{0}\\ 0 & 0 & \bar{0} & 0 & \bar{1} & 0 & 0 & 0\\ 0 & 0 & 0 & \bar{1} & 0 & \bar{0} & 0 & 0\\ 0 & 0 & \bar{1} & 0 & \bar{0} & 0 & 0 & 0\\ 0 & 0 & 0 & \bar{0} & 0 & \bar{1} & 0 & 0\\ \bar{1} & 0 & 0 & 0 & 0 & 0 & \bar{0} & 0\\ 0 & \bar{0} & 0 & 0 & 0 & 0 & 0 & \bar{1} \end{bmatrix}

Funzionerà?

unitary(circuit)
|1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0|
|0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0|
|0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0|
|0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0|
|0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0|
|0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0|
|0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1|

Funziona! Ora siamo in grado di costruire e capire circuiti facilmente, usando solo il buon senso!

EDIT: mi sono appena imbattuto in un paio di risposte su StackExchange che sembrano arrivare alla stessa conclusione per una domanda sulla porta CNOT. A quanto pare si parla di “metodo algoritmico” o di “uso dei proiettori”. Non sembra però esserci uno standard, chissà perché.

_— IceOnFire_