class TestParameterCtrlState(QiskitTestCase): """Test gate equality with ctrl_state parameter.""" @data((RXGate(0.5), CRXGate(0.5)), (RYGate(0.5), CRYGate(0.5)), (RZGate(0.5), CRZGate(0.5)), (XGate(), CXGate()), (YGate(), CYGate()), (ZGate(), CZGate()), (U1Gate(0.5), CU1Gate(0.5)), (SwapGate(), CSwapGate()), (HGate(), CHGate()), (U3Gate(0.1, 0.2, 0.3), CU3Gate(0.1, 0.2, 0.3))) @unpack def test_ctrl_state_one(self, gate, controlled_gate): """Test controlled gates with ctrl_state See https://github.com/Qiskit/qiskit-terra/pull/4025 """ self.assertEqual(gate.control(1, ctrl_state='1'), controlled_gate)
'I' : IGate(), 'H' : HGate(), 'X' : XGate(), 'Y' : YGate(), 'Z' : ZGate(), 'S' : SGate(), 'T' : TGate(), 'T_dg' : TdgGate(), 'S_dg' : SdgGate(), 'Ry' : RYGate(np.pi / 4) } CONTROLLED_GATE_DICT = { 'CX0' : CXGate(), 'CX1' : CXGate(), 'CY0' : CYGate(), 'CY1' : CYGate(), 'CZ0' : CZGate(), 'CZ1' : CYGate(), 'CH0' : CHGate(), 'CH1' : CHGate() } def _state_to_gates(state): gates = [] for qtop_gate, qbot_gate in zip(state[0], state[1]): if qtop_gate in SINGLE_GATE_DICT.keys(): gates.append([qtop_gate, [0]]) gates.append([qbot_gate, [1]]) elif qtop_gate in CONTROLLED_GATE_DICT.keys():
class TestPauli(QiskitTestCase): """Tests for Pauli operator class.""" @data(*pauli_group_labels(2)) def test_conjugate(self, label): """Test conjugate method.""" value = Pauli(label).conjugate() target = operator_from_label(label).conjugate() self.assertEqual(Operator(value), target) @data(*pauli_group_labels(2)) def test_transpose(self, label): """Test transpose method.""" value = Pauli(label).transpose() target = operator_from_label(label).transpose() self.assertEqual(Operator(value), target) @data(*pauli_group_labels(2)) def test_adjoint(self, label): """Test adjoint method.""" value = Pauli(label).adjoint() target = operator_from_label(label).adjoint() self.assertEqual(Operator(value), target) @data(*pauli_group_labels(2)) def test_inverse(self, label): """Test inverse method.""" pauli = Pauli(label) value = pauli.inverse() target = pauli.adjoint() self.assertEqual(value, target) @data(*it.product(pauli_group_labels(2, full_group=False), repeat=2)) @unpack def test_dot(self, label1, label2): """Test dot method.""" p1 = Pauli(label1) p2 = Pauli(label2) value = Operator(p1.dot(p2)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.dot(op2) self.assertEqual(value, target) @data(*pauli_group_labels(1)) def test_dot_qargs(self, label2): """Test dot method with qargs.""" label1 = '-iXYZ' p1 = Pauli(label1) p2 = Pauli(label2) qargs = [0] value = Operator(p1.dot(p2, qargs=qargs)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.dot(op2, qargs=qargs) self.assertEqual(value, target) @data(*it.product(pauli_group_labels(2, full_group=False), repeat=2)) @unpack def test_compose(self, label1, label2): """Test compose method.""" p1 = Pauli(label1) p2 = Pauli(label2) value = Operator(p1.compose(p2)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.compose(op2) self.assertEqual(value, target) @data(*pauli_group_labels(1)) def test_compose_qargs(self, label2): """Test compose method with qargs.""" label1 = '-XYZ' p1 = Pauli(label1) p2 = Pauli(label2) qargs = [0] value = Operator(p1.compose(p2, qargs=qargs)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.compose(op2, qargs=qargs) self.assertEqual(value, target) @data(*it.product(pauli_group_labels(1, full_group=False), repeat=2)) @unpack def test_tensor(self, label1, label2): """Test tensor method.""" p1 = Pauli(label1) p2 = Pauli(label2) value = Operator(p1.tensor(p2)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.tensor(op2) self.assertEqual(value, target) @data(*it.product(pauli_group_labels(1, full_group=False), repeat=2)) @unpack def test_expand(self, label1, label2): """Test expand method.""" p1 = Pauli(label1) p2 = Pauli(label2) value = Operator(p1.expand(p2)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.expand(op2) self.assertEqual(value, target) @data('II', 'XI', 'YX', 'ZZ', 'YZ') def test_power(self, label): """Test power method.""" iden = Pauli('II') op = Pauli(label) self.assertTrue(op**2, iden) @data(1, 1.0, -1, -1.0, 1j, -1j) def test_multiply(self, val): """Test multiply method.""" op = val * Pauli(([True, True], [False, False], 0)) phase = (-1j)**op.phase self.assertEqual(phase, val) def test_multiply_except(self): """Test multiply method raises exceptions.""" op = Pauli('XYZ') self.assertRaises(QiskitError, op._multiply, 2) @data(0, 1, 2, 3) def test_negate(self, phase): """Test negate method""" op = Pauli(([False], [True], phase)) neg = -op self.assertTrue(op.equiv(neg)) self.assertEqual(neg.phase, (op.phase + 2) % 4) @data(*it.product(pauli_group_labels(1, False), repeat=2)) @unpack def test_commutes(self, p1, p2): """Test commutes method""" P1 = Pauli(p1) P2 = Pauli(p2) self.assertEqual(P1.commutes(P2), P1.dot(P2) == P2.dot(P1)) @data(*it.product(pauli_group_labels(1, False), repeat=2)) @unpack def test_anticommutes(self, p1, p2): """Test anticommutes method""" P1 = Pauli(p1) P2 = Pauli(p2) self.assertEqual(P1.anticommutes(P2), P1.dot(P2) == -P2.dot(P1)) @data(*it.product( (IGate(), XGate(), YGate(), ZGate(), HGate(), SGate(), SdgGate()), pauli_group_labels(1, False))) @unpack def test_evolve_clifford1(self, gate, label): """Test evolve method for 1-qubit Clifford gates.""" cliff = Clifford(gate) op = Operator(gate) pauli = Pauli(label) value = Operator(pauli.evolve(cliff)) target = op.adjoint().dot(pauli).dot(op) self.assertEqual(value, target) @data(*it.product((CXGate(), CYGate(), CZGate(), SwapGate()), pauli_group_labels(2, False))) @unpack def test_evolve_clifford2(self, gate, label): """Test evolve method for 2-qubit Clifford gates.""" cliff = Clifford(gate) op = Operator(gate) pauli = Pauli(label) value = Operator(pauli.evolve(cliff)) target = op.adjoint().dot(pauli).dot(op) self.assertEqual(value, target) def test_evolve_clifford_qargs(self): """Test evolve method for random Clifford""" cliff = random_clifford(3, seed=10) op = Operator(cliff) pauli = random_pauli(5, seed=10) qargs = [3, 0, 1] value = Operator(pauli.evolve(cliff, qargs=qargs)) target = Operator(pauli).compose(op.adjoint(), qargs=qargs).dot(op, qargs=qargs) self.assertEqual(value, target)
def test_controlled_y(self): """Test creation of controlled y gate""" self.assertEqual(YGate().control(), CYGate())
class TestPauli(QiskitTestCase): """Tests for Pauli operator class.""" @data(*pauli_group_labels(2)) def test_conjugate(self, label): """Test conjugate method.""" value = Pauli(label).conjugate() target = operator_from_label(label).conjugate() self.assertEqual(Operator(value), target) @data(*pauli_group_labels(2)) def test_transpose(self, label): """Test transpose method.""" value = Pauli(label).transpose() target = operator_from_label(label).transpose() self.assertEqual(Operator(value), target) @data(*pauli_group_labels(2)) def test_adjoint(self, label): """Test adjoint method.""" value = Pauli(label).adjoint() target = operator_from_label(label).adjoint() self.assertEqual(Operator(value), target) @data(*pauli_group_labels(2)) def test_inverse(self, label): """Test inverse method.""" pauli = Pauli(label) value = pauli.inverse() target = pauli.adjoint() self.assertEqual(value, target) @data(*it.product(pauli_group_labels(2, full_group=False), repeat=2)) @unpack def test_dot(self, label1, label2): """Test dot method.""" p1 = Pauli(label1) p2 = Pauli(label2) value = Operator(p1.dot(p2)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.dot(op2) self.assertEqual(value, target) @data(*pauli_group_labels(1)) def test_dot_qargs(self, label2): """Test dot method with qargs.""" label1 = "-iXYZ" p1 = Pauli(label1) p2 = Pauli(label2) qargs = [0] value = Operator(p1.dot(p2, qargs=qargs)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.dot(op2, qargs=qargs) self.assertEqual(value, target) @data(*it.product(pauli_group_labels(2, full_group=False), repeat=2)) @unpack def test_compose(self, label1, label2): """Test compose method.""" p1 = Pauli(label1) p2 = Pauli(label2) value = Operator(p1.compose(p2)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.compose(op2) self.assertEqual(value, target) @data(*pauli_group_labels(1)) def test_compose_qargs(self, label2): """Test compose method with qargs.""" label1 = "-XYZ" p1 = Pauli(label1) p2 = Pauli(label2) qargs = [0] value = Operator(p1.compose(p2, qargs=qargs)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.compose(op2, qargs=qargs) self.assertEqual(value, target) @data(*it.product(pauli_group_labels(1, full_group=False), repeat=2)) @unpack def test_tensor(self, label1, label2): """Test tensor method.""" p1 = Pauli(label1) p2 = Pauli(label2) value = Operator(p1.tensor(p2)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.tensor(op2) self.assertEqual(value, target) @data(*it.product(pauli_group_labels(1, full_group=False), repeat=2)) @unpack def test_expand(self, label1, label2): """Test expand method.""" p1 = Pauli(label1) p2 = Pauli(label2) value = Operator(p1.expand(p2)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.expand(op2) self.assertEqual(value, target) @data("II", "XI", "YX", "ZZ", "YZ") def test_power(self, label): """Test power method.""" iden = Pauli("II") op = Pauli(label) self.assertTrue(op**2, iden) @data(1, 1.0, -1, -1.0, 1j, -1j) def test_multiply(self, val): """Test multiply method.""" op = val * Pauli(([True, True], [False, False], 0)) phase = (-1j)**op.phase self.assertEqual(phase, val) def test_multiply_except(self): """Test multiply method raises exceptions.""" op = Pauli("XYZ") self.assertRaises(QiskitError, op._multiply, 2) @data(0, 1, 2, 3) def test_negate(self, phase): """Test negate method""" op = Pauli(([False], [True], phase)) neg = -op self.assertTrue(op.equiv(neg)) self.assertEqual(neg.phase, (op.phase + 2) % 4) @data(*it.product(pauli_group_labels(1, False), repeat=2)) @unpack def test_commutes(self, p1, p2): """Test commutes method""" P1 = Pauli(p1) P2 = Pauli(p2) self.assertEqual(P1.commutes(P2), P1.dot(P2) == P2.dot(P1)) @data(*it.product(pauli_group_labels(1, False), repeat=2)) @unpack def test_anticommutes(self, p1, p2): """Test anticommutes method""" P1 = Pauli(p1) P2 = Pauli(p2) self.assertEqual(P1.anticommutes(P2), P1.dot(P2) == -P2.dot(P1)) @data(*it.product( (IGate(), XGate(), YGate(), ZGate(), HGate(), SGate(), SdgGate()), pauli_group_labels(1, False), )) @unpack def test_evolve_clifford1(self, gate, label): """Test evolve method for 1-qubit Clifford gates.""" op = Operator(gate) pauli = Pauli(label) value = Operator(pauli.evolve(gate)) value_h = Operator(pauli.evolve(gate, frame="h")) value_s = Operator(pauli.evolve(gate, frame="s")) value_inv = Operator(pauli.evolve(gate.inverse())) target = op.adjoint().dot(pauli).dot(op) self.assertEqual(value, target) self.assertEqual(value, value_h) self.assertEqual(value_inv, value_s) @data(*it.product((CXGate(), CYGate(), CZGate(), SwapGate()), pauli_group_labels(2, False))) @unpack def test_evolve_clifford2(self, gate, label): """Test evolve method for 2-qubit Clifford gates.""" op = Operator(gate) pauli = Pauli(label) value = Operator(pauli.evolve(gate)) value_h = Operator(pauli.evolve(gate, frame="h")) value_s = Operator(pauli.evolve(gate, frame="s")) value_inv = Operator(pauli.evolve(gate.inverse())) target = op.adjoint().dot(pauli).dot(op) self.assertEqual(value, target) self.assertEqual(value, value_h) self.assertEqual(value_inv, value_s) def test_evolve_clifford_qargs(self): """Test evolve method for random Clifford""" cliff = random_clifford(3, seed=10) op = Operator(cliff) pauli = random_pauli(5, seed=10) qargs = [3, 0, 1] value = Operator(pauli.evolve(cliff, qargs=qargs)) value_h = Operator(pauli.evolve(cliff, qargs=qargs, frame="h")) value_s = Operator(pauli.evolve(cliff, qargs=qargs, frame="s")) value_inv = Operator(pauli.evolve(cliff.adjoint(), qargs=qargs)) target = Operator(pauli).compose(op.adjoint(), qargs=qargs).dot(op, qargs=qargs) self.assertEqual(value, target) self.assertEqual(value, value_h) self.assertEqual(value_inv, value_s) def test_barrier_delay_sim(self): """Test barrier and delay instructions can be simulated""" target_circ = QuantumCircuit(2) target_circ.x(0) target_circ.y(1) target = Pauli(target_circ) circ = QuantumCircuit(2) circ.x(0) circ.delay(100, 0) circ.barrier([0, 1]) circ.y(1) value = Pauli(circ) self.assertEqual(value, target)