def test_two_qubit_gate_validate_pass(): class Dummy(gate_features.TwoQubitGate): def matrix(self): pass g = Dummy() q1 = raw_types.QubitId() q2 = raw_types.QubitId() q3 = raw_types.QubitId() g.validate_args([q1, q2]) g.validate_args([q2, q3]) g.validate_args([q3, q2])
def test_composite_gate_from_gate_tuples(): class G1(raw_types.Gate): pass class G2(raw_types.Gate): pass gates = [(G1(), (0, )), (G2(), (0, 1))] composite = gate_features.CompositeGate.from_gates(gates) q1 = raw_types.QubitId() q2 = raw_types.QubitId() assert ([gates[0][0](q1), gates[1][0](q1, q2)] == composite.default_decompose([q1, q2]))
def test_single_qubit_gate_validate_args(): class Dummy(gate_features.SingleQubitGate): def matrix(self): pass g = Dummy() q1 = raw_types.QubitId() q2 = raw_types.QubitId() g.validate_args([q1]) g.validate_args([q2]) with pytest.raises(ValueError): g.validate_args([]) with pytest.raises(ValueError): g.validate_args([q1, q2])
def test_two_qubit_gate_validate_wrong_number(): class Dummy(gate_features.TwoQubitGate): def matrix(self): pass g = Dummy() q1 = raw_types.QubitId() q2 = raw_types.QubitId() q3 = raw_types.QubitId() with pytest.raises(ValueError): g.validate_args([]) with pytest.raises(ValueError): g.validate_args([q1]) with pytest.raises(ValueError): g.validate_args([q1, q2, q3])
def test_composite_gate_from_gates(): class G1(raw_types.Gate): pass class G2(raw_types.Gate): pass gates = [G1(), G2()] composite = gate_features.CompositeGate.from_gates(gates) q1 = raw_types.QubitId() assert [gates[0](q1), gates[1](q1)] == composite.default_decompose([q1])
def matrix(self) -> np.ndarray: mat = np.eye(2) qubit = raw_types.QubitId() for op in op_tree.flatten_op_tree(self.default_decompose((qubit, ))): mat = protocols.unitary(op).dot(mat) return mat
def matrix(self) -> np.ndarray: mat = np.eye(2) qubit = raw_types.QubitId() for op in op_tree.flatten_op_tree(self.default_decompose((qubit, ))): mat = cast(gate_features.KnownMatrix, op).matrix().dot(mat) return mat