Skip to content

Commit

Permalink
Implement test for cat decomposition.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkoziell committed Sep 9, 2024
1 parent 24216b4 commit 1a963de
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions tests/test_simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from fractions import Fraction
import unittest
import sys
import random
Expand All @@ -25,7 +24,13 @@
sys.path.append('..')
sys.path.append('.')
from pyzx.circuit import Circuit
from pyzx.simulate import replace_magic_states, cut_vertex, cut_edge
from pyzx.graph import Graph, EdgeType, Scalar
from pyzx.simulate import (
replace_magic_states,
cut_vertex,
cut_edge,
gen_catlike_term
)
from pyzx.generate import cliffords
from pyzx.simplify import full_reduce

Expand Down Expand Up @@ -81,6 +86,37 @@ def test_edge_cut(self,repeats=20):
scalCut = round_complex(g0.scalar.to_number()+g1.scalar.to_number(),3) # the sum of scalars from the cut graph
assert(scal == scalCut)

def test_cat_decomp_scalar(self) -> None:
"""Test the scalars of generated cat-like terms are correct."""
g = Graph()

# Generate random scalar parameters
phase = Fraction(np.random.randint(1, 10), np.random.randint(1, 10))
power = np.random.randint(1, 10)
positive = np.random.randint(0, 1)

# Generate cat-like term with random scalar parameters
G = gen_catlike_term(g, [],
Fraction(1, 1),
Fraction(1, 1),
Fraction(1, 1),
EdgeType.SIMPLE,
EdgeType.SIMPLE,
positive,
power,
phase,
False)

# Create expected scalar
s = Scalar()
s.add_power(power)
s.add_phase(phase)
if not positive:
s.add_phase(1)

# Check if the scalar from generated term is correct
self.assertTrue(np.allclose(G.scalar.to_number(), s.to_number()))


if __name__ == '__main__':
unittest.main()

0 comments on commit 1a963de

Please sign in to comment.