OpenQASM New Gate

The U Gate and the CX Gate are the only built-in Gates provided by OpenQASM. Though it is possible to perform any Quantum Operation using these two gates, it can often interfere with the understandability of Quantum Program. OpenQASM provides the functionality of creating a new Quantum Gate using the built-in Quantum Gates. Creating a new Quantum Gate improves the understandability of the Quantum Program and makes it more concise. In this chapter of the OpenQASM Tutorial, you will be learning about how to create a new Quantum Gate.

Creating a New Gate

The following is the syntax of creating a new Quantum Gate

gate name(parameters) quantumarguments
{
    // body/definition of the gate
}

where name refers to the name of the new Quantum Gate that we are creating, parameters is a comma separated list of variable parameter names, and quantumarguments is a comma separated list of Qubit arguments.

The following are the rules for creating new Quantum Gates-

  1. The parameters, quantumarguments should contains a list of valid identifier names.
  2. The name of the Gate should be a valid identifier name.
  3. If the new Quantum Gate has no variable parameters, the parentheses are optional.
  4. There should be at least one quantum argument.
  5. The body of a Quantum Gate can contain only built-in statements/ gates, barrier operation, or previously defined gates.
  6. The arguments in quantumarguments cannot be indexed withing the body of the Gate deinition.
  7. The scope of parameters and quantumarguments is only within the definition of the Gate, and the statements inside the Gate definition can only refer to symbols given in the parameters or quantumarguments.
  8. A Gate cannot call/apply itself inside the body.

Example

Below is an example of creating a new gate, the H Gate by using the built-in U Gate. Remember that a H Gate is a U Gate with parameters θ = π/2, φ = 0 , λ = π.

The name of the new Quantum Gate that we create will be h – The Hadamard Gate. The Hadamard Operation requires no parameter, it just moves the state vector on the Bloch Sphere by π radians along the XZ axis, therefore the h Gate will have no parameters. The Hadamard Gate is a Quantum Gate that is applied on a single Qubit, therefore there will be only one quantum argument- an identifier for the Qubit on which it is to be applied. The h Gate will therefore have only one quantum argument- q1.

// Creating the h Gate
gate h q1
{
    U(pi/2, 0, pi) q1;
}

Example

Below is an example of creating a new gate, the I Gate or the Identity Gate. The name of the I Gate will be i.

gate i q1{

}

Note– Since the identity Gate performs no operation on the state of the Qubit, the body of the i Gate is empty.

Example

Below is an example of creating a new parameterized gate, the RX Gate which performs a rotation about the X axis of the Bloch Sphere by a given amount.

gate rx(theta) q1{
    U(theta, -pi/2, pi/2) q1;
}

where theta is the angle of rotation about the X axis in radians.

Applying the Gate

The obvious reason for creating a new Quantum Gate is to use it in a Quantum Program. Using a newly created Gate in a program is as easy as it can get. The following syntax is used for applying a Quantum Gate.

gate(parameters) quantumarguments;

where gate is the name of the Quantum Gate that has been created, and parameters and quantumarguments refer to the parameters and arguments being passed to the Gate.

Example

Below is an example of making use of the h Gate we created in an earlier example on the first Qubit of Quantum Register qubits.

h qubits[0];

Example

Below is an example of making use of the rx Gate we created in an earlier example on the first Qubit of Quantum Register qubits with parameter π/2.

rx(pi/2) qubits[0];