In [1]:
"""Demonstration of quantum gate identity search."""

from sympy.physics.quantum.gate import (X, Y, Z, H, S, T, CNOT,
        IdentityGate, CGate, gate_simp)
from sympy.physics.quantum.identitysearch import *
from sympy.physics.quantum.dagger import Dagger

In [2]:
# Declare a few quantum gates
x = X(0)
y = Y(0)
z = Z(0)
h = H(0)
cnot = CNOT(1,0)
cgate_z = CGate((0,), Z(1))

In [6]:
# Start with the trivial cases
gate_list = [x]

bfs_identity_search(gate_list, 1, max_depth=2)


Out[6]:
set([GateIdentity(X(0), X(0))])

In [7]:
gate_list = [y]

bfs_identity_search(gate_list, 1, max_depth=2)


Out[7]:
set([GateIdentity(Y(0), Y(0))])

In [9]:
# bfs_identity_search looks for circuits that reduce to a
# scalar value unless told otherwise.
# The following list should produce 4 identities as a result.
gate_list = [x, y, z]

bfs_identity_search(gate_list, 2)


Out[9]:
set([GateIdentity(X(0), X(0)),
     GateIdentity(Z(0), Z(0)),
     GateIdentity(X(0), Y(0), Z(0)),
     GateIdentity(Y(0), Y(0))])

In [10]:
gate_list = [x, y, z, h]

bfs_identity_search(gate_list, 2)


Out[10]:
set([GateIdentity(Y(0), H(0), Y(0), H(0)),
     GateIdentity(X(0), Y(0), X(0), Y(0)),
     GateIdentity(X(0), Y(0), Z(0)),
     GateIdentity(X(0), H(0), Z(0), H(0)),
     GateIdentity(Z(0), Z(0)),
     GateIdentity(X(0), X(0)),
     GateIdentity(Y(0), Y(0)),
     GateIdentity(X(0), Z(0), X(0), Z(0)),
     GateIdentity(Y(0), Z(0), Y(0), Z(0)),
     GateIdentity(H(0), H(0))])

In [12]:
# One has the option to limit the max size of the circuit.
# The default size is the size of the gate list.
bfs_identity_search(gate_list, 2, max_depth=3)


Out[12]:
set([GateIdentity(X(0), X(0)),
     GateIdentity(X(0), Y(0), Z(0)),
     GateIdentity(Z(0), Z(0)),
     GateIdentity(H(0), H(0)),
     GateIdentity(Y(0), Y(0))])

In [13]:
# One also has the option to find circuits that only reduce
# to the Identity matrix rather than only scalar matrices.
bfs_identity_search(gate_list, 2, identity_only=True)


Out[13]:
set([GateIdentity(X(0), X(0)),
     GateIdentity(Z(0), Z(0)),
     GateIdentity(H(0), H(0)),
     GateIdentity(Y(0), Y(0))])

In [14]:
gate_list = [cnot, cgate_z, h]

bfs_identity_search(gate_list, 2, max_depth=4)


Out[14]:
set([GateIdentity(CNOT(1,0), H(0), C((0),Z(1)), H(0)),
     GateIdentity(H(0), H(0)),
     GateIdentity(C((0),Z(1)), C((0),Z(1))),
     GateIdentity(CNOT(1,0), CNOT(1,0))])