question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Defining Permutation Gates Array is not unitary in pyquil/docs/source/basics.rst

See original GitHub issue

Pre-Report Checklist

  • I am running the latest versions of pyQuil and the Forest SDK
  • I checked to make sure that this bug has not already been reported

Issue Description

Code block

import numpy as np
from pyquil.quilbase import DefGate

ccnot_matrix = np.array([
    [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, 0, 1],
    [1, 0, 0, 0, 0, 0, 1, 0]
])

ccnot_matrix is not a unitary matrix.

>>> ccnot_gate = DefGate("CCNOT", ccnot_matrix)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/fieldofnodes/anaconda3/envs/Rigetti/lib/python3.9/site-packages/pyquil/quilbase.py", line 437, in __init__
    raise ValueError("Matrix must be unitary.")
ValueError: Matrix must be unitary.

If you take the conjugate transpose (transpose then complex conjugate of the given matrix), then multiply we should get the identity.

Suppose for a matrix $U$, then the conjugate transpose $U^\dagger$, we should have

$UU^\dagger = U^\dagger U = UU^{-1} = I,$

$I$ being the identity matrix.

ccnot_conjugate_transpose = ccnot_matrix.transpose()
ccnot_inverse = np.linalg.inv(ccnot_matrix) 

unitary_check1 = np.matmul(ccnot_matrix,ccnot_conjugate_transpose)
unitary_check2 = np.matmul(ccnot_conjugate_transpose,ccnot_matrix)
unitary_check3 = np.matmul(ccnot_matrix,ccnot_inverse)

np.array_equal(unitary_check1, unitary_check2)
np.array_equal(unitary_check1, unitary_check3)
np.array_equal(unitary_check2, unitary_check3)

>>> np.array_equal(unitary_check1, unitary_check2)
False
>>> np.array_equal(unitary_check1, unitary_check3)
False
>>> np.array_equal(unitary_check2, unitary_check3)
False

Also, check the identity matrix.

ccnot_size = ccnot_conjugate_transpose.shape[1]
identity = np.eye(8)

>>> np.array_equal(unitary_check1, identity)
False
>>> np.array_equal(unitary_check2, identity)
False
>>> np.array_equal(unitary_check2, identity)
False
----------------

If useful, provide a numbered list of the steps that result in the error.

Otherwise, just fill out the "Code Snippet" and "Error Output" sections below.

### Code Snippet

```python
Include a snippet of the pyQuil code that produces the error here.

Error Output

Additionally, please copy and paste the output of the above code here.

Environment Context

Operating System:

Python Version (python -V):

Quilc Version (quilc --version):

QVM Version (qvm --version):

Python Environment Details (pip freeze or conda list):

Copy and paste the output of `pip freeze` or `conda list` here.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fieldofnodescommented, Aug 11, 2022

Here: https://pyquil-docs.rigetti.com/en/stable/basics.html This is the stable version

Go to Defining Permutation Gates

Note the matrx.

1reaction
caldwellshanecommented, Aug 9, 2022

@fieldofnodes It looks like there is simply a typo in your definition of CCNOT. The bottom row should start with a 0 instead of a 1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Source code for pyquil.unitary_tools - Rigetti Computing
However, for a multiqubit gate acting on non-adjactent qubit indices, we must first apply a permutation matrix to make the qubits adjacent and...
Read more >
Quantum Circuits for the Unitary Permutation Problem - arXiv
Abstract: We consider the Unitary Permutation problem which consists, given n unitary gates U_1, \ldots, U_n and a permutation \sigma of \{1 ...
Read more >
UnitaryGate - Qiskit
Class quantum gates specified by a unitary matrix. Example. We can create a unitary gate from a unitary matrix then add it to...
Read more >
How to create unitary gate from np.array in qiskit?
I believe your implementation with qc.unitary(U2x2, range(1)) is correct if you just want a regular unitary gate from an array.
Read more >
Check if an Array is a permutation of numbers from 1 to N
Given an array arr containing N positive integers, the task is to check if the given array arr represents a permutation or not....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found