"""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
# 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))
# Start with the trivial cases
gate_list = [x]
bfs_identity_search(gate_list, 1, max_depth=2)
set([GateIdentity(X(0), X(0))])
gate_list = [y]
bfs_identity_search(gate_list, 1, max_depth=2)
set([GateIdentity(Y(0), Y(0))])
# 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)
set([GateIdentity(X(0), X(0)), GateIdentity(Z(0), Z(0)), GateIdentity(X(0), Y(0), Z(0)), GateIdentity(Y(0), Y(0))])
gate_list = [x, y, z, h]
bfs_identity_search(gate_list, 2)
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))])
# 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)
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))])
# 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)
set([GateIdentity(X(0), X(0)), GateIdentity(Z(0), Z(0)), GateIdentity(H(0), H(0)), GateIdentity(Y(0), Y(0))])
gate_list = [cnot, cgate_z, h]
bfs_identity_search(gate_list, 2, max_depth=4)
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))])