Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
test_partition.py
Go to the documentation of this file.
1 import pytest
2 import numpy as np
3 from squander import Circuit
4 from squander import utils
5 from squander.partitioning.partition import (
6  get_qubits,
7  kahn_partition,
8  translate_param_order,
9  PartitionCircuitQasm
10 )
11 
12 """
13 CORRECTNESS TESTS
14 """
15 
16 @pytest.mark.parametrize("max_qubits", [3, 4, 5])
18  empty_c = Circuit(5)
19  top_c, param_order, _ = kahn_partition(empty_c, max_qubits)
20  assert len(top_c.get_Gates()) == 1 # NOTE: should be 0
21  assert len(param_order) == 0
22 
23 
24 @pytest.mark.parametrize("max_qubits", [3, 4, 5])
25 def test_PartitionSingleGate(max_qubits):
26  single_c = Circuit(5)
27  single_c.add_CNOT(0, 1)
28  top_c, param_order, _ = kahn_partition(single_c, max_qubits)
29  assert len(top_c.get_Gates()) == 1
30  assert len(param_order) == 1
31 
32 
33 @pytest.mark.parametrize("max_qubits", [3, 4, 5])
34 def test_PartitionTotalGates(max_qubits):
35  c = Circuit(5)
36  c.add_CNOT(0, 1)
37  c.add_CNOT(1, 2)
38  c.add_CNOT(2, 3)
39  top_c, _, _ = kahn_partition(c, max_qubits)
40  total_gates = sum(len(p.get_Gates()) for p in top_c.get_Gates())
41  assert total_gates == len(c.get_Gates())
42 
43 
44 @pytest.mark.parametrize("max_qubits", [3, 4, 5])
46  c = Circuit(5)
47  c.add_CNOT(0, 1)
48  c.add_CNOT(1, 2)
49  c.add_CNOT(2, 3)
50  top_c, _, _ = kahn_partition(c, max_qubits)
51  for p in top_c.get_Gates():
52  qubits = set.union(*(get_qubits(gate) for gate in p.get_Gates()))
53  assert len(qubits) <= max_qubits
54 
55 
56 @pytest.mark.parametrize("max_qubits", [3, 4, 5])
58  c = Circuit(max_qubits)
59  c.add_CNOT(0, 1)
60  c.add_CNOT(1, 2)
61  top_c, _, _ = kahn_partition(c, max_qubits)
62  assert len(top_c.get_Gates()) == 1
63 
64 
65 
66 
68  filename = "examples/partitioning/qasm_samples/heisenberg-16-20.qasm"
69 
70  initial_circuit, initial_parameters = utils.qasm_to_squander_circuit(filename)
71 
72  max_partition_size = 4
73  partitined_circuit, partitioned_parameters = PartitionCircuitQasm( filename, max_partition_size )
74 
75 
76  # generate random initial state on which we test the circuits
77  qbit_num = initial_circuit.get_Qbit_Num()
78 
79 
80  matrix_size = 1 << qbit_num
81  initial_state_real = np.random.uniform(-1.0,1.0, (matrix_size,) )
82  initial_state_imag = np.random.uniform(-1.0,1.0, (matrix_size,) )
83  initial_state = initial_state_real + initial_state_imag*1j
84  initial_state = initial_state/np.linalg.norm(initial_state)
85 
86 
87 
88  transformed_state_1 = initial_state.copy()
89  transformed_state_2 = initial_state.copy()
90 
91  initial_circuit.apply_to( initial_parameters, transformed_state_1 )
92  partitined_circuit.apply_to( partitioned_parameters, transformed_state_2)
93 
94  diff = np.linalg.norm( transformed_state_1 - transformed_state_2 )
95 
96  assert( diff < 1e-10 )
97 
98 
99 
100 
101 
102 
def test_PartitionTotalGates(max_qubits)
def PartitionCircuitQasm
Definition: partition.py:242
def test_PartitionSingleGate(max_qubits)
def get_qubits(gate)
Definition: partition.py:12
def test_PartitionMaxQubitConstraint(max_qubits)
def kahn_partition(c, max_qubit, preparts=None)
Definition: partition.py:25
def test_CorrectnessOfPartitionedCircuit()
def test_PartitionMaxQubitsEqualsTotalQubits(max_qubits)
def test_PartitionEmptyCircuit(max_qubits)