def test_adding_gate_and_partially_specified_gate(self): """Verify entries will different numbers of parameters will be returned.""" eq_lib = EquivalenceLibrary() theta = Parameter('theta') phi = Parameter('phi') # e.g. RGate(theta, phi) gate_full = OneQubitTwoParamGate(theta, phi) equiv_full = QuantumCircuit(1) equiv_full.append(U2Gate(theta, phi), [0]) eq_lib.add_equivalence(gate_full, equiv_full) gate_partial = OneQubitTwoParamGate(theta, 0) equiv_partial = QuantumCircuit(1) equiv_partial.rx(theta, 0) eq_lib.add_equivalence(gate_partial, equiv_partial) lam = Parameter('lam') gate_query = OneQubitTwoParamGate(lam, 0) entry = eq_lib.get_entry(gate_query) first_expected = QuantumCircuit(1) first_expected.append(U2Gate(lam, 0), [0]) second_expected = QuantumCircuit(1) second_expected.rx(lam, 0) self.assertEqual(len(entry), 2) self.assertEqual(entry[0], first_expected) self.assertEqual(entry[1], second_expected)
def test_adding_gate_under_different_parameters(self): """Verify a gate can be added under different sets of parameters.""" eq_lib = EquivalenceLibrary() theta = Parameter('theta') gate_theta = OneQubitOneParamGate(theta) equiv_theta = QuantumCircuit(1) equiv_theta.p(theta, 0) eq_lib.add_equivalence(gate_theta, equiv_theta) phi = Parameter('phi') gate_phi = OneQubitOneParamGate(phi) equiv_phi = QuantumCircuit(1) equiv_phi.rz(phi, 0) eq_lib.add_equivalence(gate_phi, equiv_phi) lam = Parameter('lam') gate_query = OneQubitOneParamGate(lam) entry = eq_lib.get_entry(gate_query) first_expected = QuantumCircuit(1) first_expected.p(lam, 0) second_expected = QuantumCircuit(1) second_expected.rz(lam, 0) self.assertEqual(len(entry), 2) self.assertEqual(entry[0], first_expected) self.assertEqual(entry[1], second_expected)
def test_create_empty_library(self): """An empty library should return an empty entry.""" eq_lib = EquivalenceLibrary() self.assertIsInstance(eq_lib, EquivalenceLibrary) gate = OneQubitZeroParamGate() entry = eq_lib.get_entry(gate) self.assertEqual(len(entry), 0)
def test_create_empty_library_with_base(self): """Verify retrieving from an empty library returns an empty entry.""" base = EquivalenceLibrary() eq_lib = EquivalenceLibrary(base=base) self.assertIsInstance(eq_lib, EquivalenceLibrary) gate = OneQubitZeroParamGate() entry = eq_lib.get_entry(gate) self.assertEqual(len(entry), 0)
def test_add_single_entry(self): """Verify an equivalence added to the library can be retrieved.""" eq_lib = EquivalenceLibrary() gate = OneQubitZeroParamGate() equiv = QuantumCircuit(1) equiv.h(0) eq_lib.add_equivalence(gate, equiv) entry = eq_lib.get_entry(gate) self.assertEqual(len(entry), 1) self.assertIsNot(entry[0], equiv) self.assertEqual(entry[0], equiv)
def test_get_through_empty_library_to_base(self): """Verify we find an entry defined only in the base library.""" base = EquivalenceLibrary() gate = OneQubitZeroParamGate() equiv = QuantumCircuit(1) equiv.h(0) base.add_equivalence(gate, equiv) eq_lib = EquivalenceLibrary(base=base) entry = eq_lib.get_entry(gate) self.assertEqual(len(entry), 1) self.assertIsNot(entry[0], equiv) self.assertEqual(entry[0], equiv)
def test_set_entry(self): """Verify setting an entry overrides any previously added.""" eq_lib = EquivalenceLibrary() gate = OneQubitZeroParamGate() first_equiv = QuantumCircuit(1) first_equiv.h(0) eq_lib.add_equivalence(gate, first_equiv) second_equiv = QuantumCircuit(1) second_equiv.append(U2Gate(0, np.pi), [0]) eq_lib.set_entry(gate, [second_equiv]) entry = eq_lib.get_entry(gate) self.assertEqual(len(entry), 1) self.assertEqual(entry[0], second_equiv)
def test_add_double_entry(self): """Verify separately added equivalences can be retrieved.""" eq_lib = EquivalenceLibrary() gate = OneQubitZeroParamGate() first_equiv = QuantumCircuit(1) first_equiv.h(0) eq_lib.add_equivalence(gate, first_equiv) second_equiv = QuantumCircuit(1) second_equiv.append(U2Gate(0, np.pi), [0]) eq_lib.add_equivalence(gate, second_equiv) entry = eq_lib.get_entry(gate) self.assertEqual(len(entry), 2) self.assertEqual(entry[0], first_equiv) self.assertEqual(entry[1], second_equiv)
def test_set_entry(self): """Verify we find only equivalences from top when explicitly set.""" base = EquivalenceLibrary() gate = OneQubitZeroParamGate() first_equiv = QuantumCircuit(1) first_equiv.h(0) base.add_equivalence(gate, first_equiv) eq_lib = EquivalenceLibrary(base=base) second_equiv = QuantumCircuit(1) second_equiv.append(U2Gate(0, np.pi), [0]) eq_lib.set_entry(gate, [second_equiv]) entry = eq_lib.get_entry(gate) self.assertEqual(len(entry), 1) self.assertEqual(entry[0], second_equiv)
def test_add_equivalence(self): """Verify we find all equivalences if a gate is added to top and base.""" base = EquivalenceLibrary() gate = OneQubitZeroParamGate() first_equiv = QuantumCircuit(1) first_equiv.h(0) base.add_equivalence(gate, first_equiv) eq_lib = EquivalenceLibrary(base=base) second_equiv = QuantumCircuit(1) second_equiv.append(U2Gate(0, np.pi), [0]) eq_lib.add_equivalence(gate, second_equiv) entry = eq_lib.get_entry(gate) self.assertEqual(len(entry), 2) self.assertEqual(entry[0], second_equiv) self.assertEqual(entry[1], first_equiv)
def test_parameter_in_parameter_out(self): """Verify query parameters will be included in returned entry.""" eq_lib = EquivalenceLibrary() theta = Parameter('theta') gate = OneQubitOneParamGate(theta) equiv = QuantumCircuit(1) equiv.p(theta, 0) eq_lib.add_equivalence(gate, equiv) phi = Parameter('phi') gate_phi = OneQubitOneParamGate(phi) entry = eq_lib.get_entry(gate_phi) expected = QuantumCircuit(1) expected.p(phi, 0) self.assertEqual(len(entry), 1) self.assertEqual(entry[0], expected)
def test_partial_parameter_in_parameter_out(self): """Verify numeric query parameters will be included in returned entry.""" eq_lib = EquivalenceLibrary() theta = Parameter('theta') phi = Parameter('phi') gate = OneQubitTwoParamGate(theta, phi) equiv = QuantumCircuit(1) equiv.u(theta, phi, 0, 0) eq_lib.add_equivalence(gate, equiv) lam = Parameter('lam') gate_partial = OneQubitTwoParamGate(lam, 1.59) entry = eq_lib.get_entry(gate_partial) expected = QuantumCircuit(1) expected.u(lam, 1.59, 0, 0) self.assertEqual(len(entry), 1) self.assertEqual(entry[0], expected)