5
$\begingroup$

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, 8x8 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} $$

$\endgroup$
  • $\begingroup$ Ideally you would do this by telling qiskit that you want the gate specified by this matrix. I'm not sure that feature exists yet, but I'm trying to find out. $\endgroup$ – James Wootton Dec 18 '18 at 10:55
  • $\begingroup$ @Nillmer Your original circuit needs this gate or you need to check if the circuit corresponds to this unitary? $\endgroup$ – cnada Dec 19 '18 at 3:16
  • $\begingroup$ @cnada I need to check if the circuit corresponds to this unitary. $\endgroup$ – Nillmer Dec 19 '18 at 11:45
  • $\begingroup$ @Nillmer See my answer and tell me if that would do the job. $\endgroup$ – cnada Dec 19 '18 at 12:44
2
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ That last column looks weird. I guess since CNOT and Z commute, it puts them on the same column regardless of whether looks like a CZ. Would prefer a diagram overlap check before it draws the circuit. $\endgroup$ – AHusain Dec 18 '18 at 23:43
  • $\begingroup$ @AHusain It's a CZ and a CNOT, not a Z and a CNOT. The overlap is intentional and means "each of those operations is controlled by the control". $\endgroup$ – Craig Gidney Dec 19 '18 at 1:22
  • $\begingroup$ Ok. Would it overlap like that in the CNOT and Z case as well? Or would they separate? Ideally the diagram should not be ambiguous as to which lines connect where. $\endgroup$ – AHusain Dec 19 '18 at 2:20
  • $\begingroup$ @AHusain If it was a Z gate it would have to be placed on a separate vertical column. Otherwise it would be ambiguous, as you note. $\endgroup$ – Craig Gidney Dec 19 '18 at 3:06
2
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ The link is not found $\endgroup$ – pushpen.paul Apr 16 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.