TransWikia.com

How do I build a gate from a matrix on Qiskit?

Quantum Computing Asked by Nillmer on December 16, 2020

I’m creating a gate for a project and need to test if it has the same results as the original circuit in a simulator, how do I build this gate on Qiskit? It’s a 3 qubit gate, 8×8 matrix:

$$
frac{1}{2}
begin{bmatrix}
1 & 0 & 1 & 0 & 0 & 1 & 0 & -1
0 & 1 & 0 & 1 & 1 & 0 & -1 & 0
0 & 1 & 0 & -1 & 1 & 0 & 1 & 0
1 & 0 & -1 & 0 & 0 & 1 & 0 & 1
1 & 0 & 1 & 0 & 0 & -1 & 0 & 1
0 & 1 & 0 & 1 & -1 & 0 & 1 & 0
0 & 1 & 0 & -1 & -1 & 0 & -1 & 0
1 & 0 & -1 & 0 & 0 & -1 & 0 & -1
end{bmatrix}
$$

5 Answers

I don't think Qiskit has this simulation feature. You have to decompose it indeed.

However, there is another way to solve your problem. To check if a quantum circuit (that you can submit in Qiskit) corresponds to a unitary matrix, you can use the unitary_simulator backend.

# Run the quantum circuit on a unitary simulator backend
backend = Aer.get_backend('unitary_simulator')
job = execute(circ, backend)
result = job.result()
print(np.around(result.get_unitary(circ), 3))

This will print the unitary matrix that your circuit represents. And you can compare to yours.

Correct answer by cnada on December 16, 2020

Qubiter uses a CSD compiler for a Unitary matrix to a sequence of elementary operations tranformation

One setback is that qubiter needs extra packages so installing could be troublesome.

Answered by Bram on December 16, 2020

Here's the circuit for your specific case:

circuit

I made it manually, by entering the matrix into Quirk, diagonalizing the matrix by adding operations, then simplifying the operations. It's not too hard to do by hand when all the operations are Clifford as in this case.

Answered by Craig Gidney on December 16, 2020

You can't directly build a gate from arbitrary matrices because custom gates need to be implemented using the build-in gates.

You have to decompose your matrix to known gates.

For a random two-qubit gate, there is two_qubit_kak:

two_qubit_kak (unitary_matrix, verify_gate_sequence=False)

Decompose a two-qubit gate over CNOT + SU(2) using the KAK decomposition.

Based on MATLAB implementation by David Gosset.

Computes a sequence of 10 single and two qubit gates, including 3 CNOTs, which multiply to U, including global phase. Uses Vatan and Williams optimal two-qubit circuit (quant-ph/0308006v3). The decomposition algorithm which achieves this is explained well in Drury and Love, 0806.4015.

Answered by Eelvex on December 16, 2020

You can build your gate with Operator and unitary function e.g:

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.quantum_info.operators import Operator

controls = QuantumRegister(2)
circuit = QuantumCircuit(controls)

cx = Operator([
    [1, 0, 0, 0],
    [0, 0, 0, 1],
    [0, 0, 1, 0],
    [0, 1, 0, 0]
])
circuit.unitary(cx, [0, 1], label='cx')

Output:
         ┌──────────┐
q0_0: |0>┤0         ├
         │  unitary │
q0_1: |0>┤1         ├
         └──────────┘

This is identical to:

circuit.cx(controls[0], controls[1])

Output:

q0_0: |0>──■──
         ┌─┴─┐
q0_1: |0>┤ X ├
         └───┘

For more info look Qiskit tutorials: https://qiskit.org/documentation/tutorials/simulators/4_custom_gate_noise.html

Answered by iUrii on December 16, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP