# How to define a custom gate from a matrix in TKET

I'm currently learning all about different quantum tools such as qiskit, tket, cirq, Forest SDK and so on. I just want to create a circuit that gets executed given a matrix, because a circuit can be written as a matrix and the other way around. In qiskit, cirq and pyquil (Forest SDK) there is no problem in defining an own gate. I don't find any function in tket which could help with this.

For example I want to define an AND gate. For those who don't know, an AND gate is actually just the CCNOT or CCX Gate. Tket does support the CCX gate but if I want to create maybe an AND Gate with 3 or 4 Qubits (CCCX or CCCCX) Gate the tket framework doesn't help.

Can somebody help me with this? I understand that you can define own gates in Tket but that just include gates that are created from native gates.

You can define custom gates in tket using CustomGateDef. Here the CustomGate is defined using a circuit which implements the desired unitary instead of the matrix itself. Here is an example for how to do this...

https://cqcl.github.io/pytket/manual/manual_circuit.html#custom-parameterised-gates

If the unitary represents a one, two or three qubit operation it is possible to synthesise a circuit to implement the unitary. See for example https://cqcl.github.io/tket/pytket/api/circuit.html#pytket.circuit.Unitary3qBox

If you're interested in multi-controlled X operations you can add these to your Circuit as a CnX.

from pytket import Circuit, OpType

circ = Circuit(4)


You may also wish to check out the ToffoliBox feature which implements an arbitrary permutation of the computational basis states using X and CnX gates.

There is an example in this section of the user manual if you scroll down. https://cqcl.github.io/pytket/manual/manual_circuit.html#boxes

• Arbitrary unitary synthesis in not implemented in TKET for > 3 qubits. A decision was made not to include this as the algorithms scale badly with respect to two-qubit gate count and runtime. As a workaround you could define your gate in qiskit and convert it to TKET using the qiskit_to_tk function from the pytket-qiskit extension. You may need to supply a method to decompose your new gate first. There are special box types that can be decomposed efficiently for a larger number of qubits. See ToffoliBox and PauliExpBox. Depending on what you are doing these could be useful. Dec 5, 2022 at 17:00