# chGate

Since R2023a

Installation Required: This functionality requires MATLAB Support Package for Quantum Computing.

## Syntax

``g = chGate(controlQubit,targetQubit)``

## Description

example

````g = chGate(controlQubit,targetQubit)` applies a controlled Hadamard gate to a target qubit based on the state of a control qubit and returns a `quantum.gate.SimpleGate` object. If the control qubit is in the $|0〉$ state, then this gate does nothing. If the control qubit is in the $|1〉$ state, then this gate applies a Hadamard gate (`hGate`) to the target qubit. If `controlQubit` and `targetQubit` are vectors of the same length, `chGate` returns a column vector of gates, where `g(i)` represents a controlled Hadamard gate applied to a control qubit with index `controlQubit(i)` and a target qubit with index `targetQubit(i)`. If either `controlQubit` or `targetQubit` is a scalar, and the other input is a vector, then MATLAB® expands the scalar to match the size of the vector input.The qubit indices in each element pair of `controlQubit` and `targetQubit` must not be the same. ```

## Examples

collapse all

Create a controlled Hadamard gate that acts on a control qubit with index 1 and a target qubit with index 2.

`g = chGate(1,2)`
```g = SimpleGate with properties: Type: "ch" ControlQubits: 1 TargetQubits: 2 Angles: [1×0 double]```

Get the matrix representation of the gate.

`M = getMatrix(g)`
```M = 1.0000 0 0 0 0 1.0000 0 0 0 0 0.7071 0.7071 0 0 0.7071 -0.7071```

Create an array of three controlled Hadamard gates. The first gate acts on control qubit 1 and target qubit 2, the next gate acts on control qubit 2 and target qubit 3, and the final gate acts on control qubit 3 and target qubit 4.

`g = chGate(1:3,2:4)`
```g = 3×1 SimpleGate array with gates: Id Gate Control Target 1 ch 1 2 2 ch 2 3 3 ch 3 4 ```

## Input Arguments

collapse all

Control qubit of the gate, specified as a positive integer scalar index or vector of qubit indices.

Example: `2`

Example: `6:8`

Target qubit of the gate, specified as a positive integer scalar index or vector of qubit indices.

Example: `1`

Example: `3:5`

collapse all

### Matrix Representation of Controlled Hadamard Gate

The matrix representation of a controlled Hadamard gate applied to control qubit 1 and target qubit 2 is

`$\left[\begin{array}{cccc}1& 0& 0& 0\\ 0& 1& 0& 0\\ 0& 0& \frac{1}{\sqrt{2}}& \frac{1}{\sqrt{2}}\\ 0& 0& \frac{1}{\sqrt{2}}& -\text{\hspace{0.17em}}\frac{1}{\sqrt{2}}\end{array}\right].$`

If the control qubit is in the $|0〉$ state, then this gate leaves the target qubit as is. If the control qubit is in the $|1〉$ state, then this gate maps the target qubit basis states from the Z basis to the X basis (and from the X basis to the Z basis)

## Version History

Introduced in R2023a