Identity Matrix

QSharp

On this page:

The Identity Matrix

The Identity matrix is used primarily to 'step-up' the size of gates to appropriately match the size of a state vector.gate acts on a single qubit. In a 2 x 2 square configuration, an Indentity matrix maps |0› to |0› and |1› to |1›. A 2 x 2 square Identity matrix is represented as:

The Identity matrix

//	One-line notation
{{1, 0}, {0, 1}}

//	Expanded notation
{
{1, 0},
{0, 1}
}

* Note that an identity matrix can be of any size. The following are all identity matrices:

{
{1, 0},
{0, 1}
}

{
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
}

{
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
}

It is the presence of 1 along the entire diagonal (and 0 everywhere else) that specifies the form on an identity matrix.

Manipulation of a register takes the form of matrix algebra. In order for the mathematics to take place, matricies of appropriate size must be constructed. This can be a tedious and time consuming process.

Thankfully, there is an easier way to compute these outcomes, as will be demonstrated.

If you are unfamiliar with either tensor products or matrix multiplication, then I would suggest revising those topics first, otherwise a lot of this will look like magic.

Syntax

The Quantum Console syntax for this operation is:

I(n);

Where n specifies the number (or index) of the qubit we wish to manipulate.

* Note that this index is 0 based, not 1 based as represented in most examples.

The Identity Matrix Short-Cut

As the I matrix takes 0 to 0, and 1 to 1, it can essentially be thought of as a non-operation. With this in mind, it is safe to ignore tensor operations and simply construct an I matrix of the approrpriate size, where the number of columns and rows match the number of elements in the state vector.

It is also safe to ignore the rest of the documentation on this page should you choose to simply construct the I directly, however examples have been provided anyway.

1 Qubit Register

Performing this calculation on a single qubit is fairly straight forward, as the rules for matrix multiplication (number of columns from matrix A must match the number of rows from matrix B) are satisfied.

In other words, the number of columns in the Identity matrix match the number of rows in the state vector.

Matrix mathematics

{
{1, 0},
{0, 1}
}
*
{
{A|0›},
{B|1›}
}
=
Show intermediate working
{
{A|0›},
{B|1›}
}

Performing the operation in Quantum Console

//	Qubits
(A|0› + B|1›)

//	Computational Basis States - Input
A|0› + B|1›

//	Quantum Console syntax
//	Apply I to qubit 0
I(0);

//	Computational Basis States - Ouput
A|0› + B|1›

2 Qubit Register

Performing this calculation becomes a little bit trickier, as our state vector is now larger (2n, n = 2) at 4 rows.

Consequently, as the state vector now has 4 rows, so an I matrix cannot be applied to it, as the number of columns from matrix A (2) does not match the number of rows from matrix B (4).

To work around this, it is necessary to tensor an I (identity) matrix for each other qubit in order to construct the matrix of the appropriate size.

In this example, the I matrix is applied to the qubit in position 1, and as the identity matrices are inferred, the operation could be written as:

II

If the I matrix was to be applied to the qubit in position 0, and as the identity matrices are inferred, the operation could be written as:

II

Matrix mathematics

First, tensor the gate with an identity matrix:

{
{1, 0},
{0, 1}
}
(x)
{
{1, 0},
{0, 1}
}
=
{
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
}

This 'steps up' the I matrix to be appropriately sized for the state vector.

Now the result can be applied to the state vector:

{
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
}
*
{
{AC|00›},
{AD|01›},
{BC|10›},
{BD|11›}
}
=
Show intermediate working
{
{AC|00›},
{AD|01›},
{BC|10›},
{BD|11›}
}

Performing the operation in Quantum Console

//	Qubits
(A|0› + B|1›) (x) (C|0› + D|1›)

//	Computational Basis States - Input
AC|00› + AD|01› + BC|10› + BD|11›

//	Quantum Console syntax
//	Apply I to qubit 1
I(1);

//	Computational Basis States - Ouput
AC|00› + AD|01› + BC|10› + BD|11›

3 Qubit Register

This time, the state vector is larger again (2n, n = 3), at 8 rows, and due to this an I matrix cannot be applied as the number of columns from matrix A (2) does not match the number of rows from matrix B (8).

To work around this, as before, tensor an I (identity) matrix for each qubit involved that is not being manipulated in order to construct the matrix of the appropriate size.

In this example, the I matrix is applied to the qubit in position 1, and as the identity matrices are inferred, the operation could be written as:

III

If the I matrix was to be applied to the qubit in position 0, and as the identity matrices are inferred, the operation could be written as:

III

Matrix mathematics

Tensor the gate with an identity.

Tensor products are produced by applying the left matrix to the right. So in this instance, the I is applied to the X first, then II to I.

I (step 1: tensor) I (step 2: tensor) I

{
{1, 0},
{0, 1}
}
(x)
{
{1, 0},
{0, 1}
}
=
{
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
}

Tensor the product with another identity:

{
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
}
(x)
{
{1, 0},
{0, 1}
}
=
{
{1, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0}
{0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 1}
}

The matrix is now the correct size to be applied to the state vector:

{
{1, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0}
{0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 1}
}
*
{
{ACE|000›},
{ACF|001›},
{ADE|010›},
{ADF|011›},
{BCE|100›},
{BCF|101›},
{BDE|110›},
{BDF|111›}
}
=
Show intermediate working
{
{ACE|000›},
{ACF|001›},
{ADE|010›},
{ADF|011›},
{BCE|100›},
{BCF|101›},
{BDE|110›},
{BDF|111›}
}

Performing the operation in Quantum Console

//	Qubits
(A|0› + B|1›) (x) (C|0› + D|1›) (x) (E|0› + F|1›)

//	Computational Basis States - Input
ACE|000› + ACF|001› + ADE|010› + ADF|011› + BCE|100› + BCF|101› + BDE|110› + BDF|111›

//	Quantum Console syntax
//	Apply I to qubit 1
I(1);

//	Computational Basis States - Ouput
ACE|000› + ACF|001› + ADE|010› + ADF|011› + BCE|100› + BCF|101› + BDE|110› + BDF|111›

n Qubit Register

As you can see from the previous examples, performing manual matrix arthimetic for each step becomes tedious and time consuming, however it is important to acknowledge that this process takes place in the first instance.

Thankfully, there is a much simpler way of working out these operations as we shall discover for an n qubit register.

Suppose an I matrix is to be applied to qubit 1 in a register, as with the previous examples.

For a 2 qubit system, apply the X logic to the each bit indicated in the operation. I(1) for example, applies an I gate to every bit in position 1.

//	State vector
|ψ› = (AC|00› + AD|01› + BC|10› + BD|11›)

//	1 bit positions bolded - these are the bits that will be affected by the I operation
(AC|00› + AD|01› + BC|10› + BD|11›)

//	Apply the I to every bit that occupies the 1 position
I(1);

//	State vector - compare this to the matrix calculations above
|ψ› = (AC|00› + AD|01› + BC|10› + BD|11›)

Using the same methodology on a larger system reduces the amount of manual work. For a 3 qubit register:

//	State vector
|ψ› = (ACE|000› + ACF|001› + ADE|010› + ADF|011› + BCE|100› + BCF|101› + BDE|110› + BDF|111›)

//	1 bit positions bolded
(ACE|000› + ACF|001› + ADE|010› + ADF|011› + BCE|100› + BCF|101› + BDE|110› + BDF|111›)

//	Apply the I to every bit that occupies the 1 position
I(1);

//	State vector - compare this to the matrix calculations above
|ψ› = (ACE|000› + ACF|001› + ADE|010› + ADF|011› + BCE|100› + BCF|101› + BDE|110› + BDF|111›)

The Column Method

You might find that using the column method is easier and faster - it took a while for me to develop, but now I find it easier to use than other methods.

To use the column method:

  1. Write the state vector vertically down the page
  2. Write an index over each bit column
  3. Write the gate over the bit to be operated on
  4. Now, work out the result of the operation, one element at a time, and write these into a new column

Applying the column method to the 3 qubit example from above:

//	I(1);
//	Apply the I gate, one element at a time

 I
012

000  000	//	Remember the I takes 0 to 0, and 1 to 1
001  001
010  010
011  011
100  100
101  101
110  110
111  111

Now, all that is left is to take the values from the left column and plug them into the right column:

     I
    012

ACE|000  ACE|000
ACF|001  ACF|001
ADE|010  ADE|010
ADF|011  ADF|011
BCE|100  BCE|100
BCF|101  BCF|101
BDE|110  BDE|110
BDF|111  BDF|111

No adjustement of the state vector (using the binary index) to match the new order is required, as the ordering of element is unchanged.

The manipulation of the state vector is now complete.


 

Copyright © 2024 carlbelle.com