def test_depolarizing_error_2q_gate(self): """Test 2-qubit depolarizing error as gate qobj""" p_depol = 0.3 with self.assertWarns(DeprecationWarning): actual = depolarizing_error(p_depol, 2, standard_gates=True) target_circs = [[{"name": "id", "qubits": [0]}, {"name": "id", "qubits": [1]}], [{"name": "id", "qubits": [0]}, {"name": "x", "qubits": [1]}], [{"name": "id", "qubits": [0]}, {"name": "y", "qubits": [1]}], [{"name": "id", "qubits": [0]}, {"name": "z", "qubits": [1]}], [{"name": "x", "qubits": [0]}, {"name": "id", "qubits": [1]}], [{"name": "x", "qubits": [0]}, {"name": "x", "qubits": [1]}], [{"name": "x", "qubits": [0]}, {"name": "y", "qubits": [1]}], [{"name": "x", "qubits": [0]}, {"name": "z", "qubits": [1]}], [{"name": "y", "qubits": [0]}, {"name": "id", "qubits": [1]}], [{"name": "y", "qubits": [0]}, {"name": "x", "qubits": [1]}], [{"name": "y", "qubits": [0]}, {"name": "y", "qubits": [1]}], [{"name": "y", "qubits": [0]}, {"name": "z", "qubits": [1]}], [{"name": "z", "qubits": [0]}, {"name": "id", "qubits": [1]}], [{"name": "z", "qubits": [0]}, {"name": "x", "qubits": [1]}], [{"name": "z", "qubits": [0]}, {"name": "y", "qubits": [1]}], [{"name": "z", "qubits": [0]}, {"name": "z", "qubits": [1]}]] target_probs = [1 - p_depol * 15 / 16] + [p_depol / 16] * 15 with self.assertWarns(DeprecationWarning): expected = QuantumError(zip(target_circs, target_probs), standard_gates=True) for i in range(actual.size): circ, prob = actual.error_term(i) expected_circ, expected_prob = expected.error_term(i) self.assertEqual(circ, expected_circ, msg=f"Incorrect {i}-th circuit") self.assertAlmostEqual(prob, expected_prob, msg=f"Incorrect {i}-th probability")
def test_depolarizing_error_2q_gate(self): """Test 2-qubit depolarizing error as gate qobj""" p_depol = 0.3 actual = depolarizing_error(p_depol, 2) expected = QuantumError([ (PauliGate("II"), 1 - p_depol * 15 / 16), (PauliGate("IX"), p_depol / 16), (PauliGate("IY"), p_depol / 16), (PauliGate("IZ"), p_depol / 16), (PauliGate("XI"), p_depol / 16), (PauliGate("XX"), p_depol / 16), (PauliGate("XY"), p_depol / 16), (PauliGate("XZ"), p_depol / 16), (PauliGate("YI"), p_depol / 16), (PauliGate("YX"), p_depol / 16), (PauliGate("YY"), p_depol / 16), (PauliGate("YZ"), p_depol / 16), (PauliGate("ZI"), p_depol / 16), (PauliGate("ZX"), p_depol / 16), (PauliGate("ZY"), p_depol / 16), (PauliGate("ZZ"), p_depol / 16), ]) for i in range(actual.size): circ, prob = actual.error_term(i) expected_circ, expected_prob = expected.error_term(i) self.assertEqual(circ, expected_circ, msg=f"Incorrect {i}-th circuit") self.assertAlmostEqual(prob, expected_prob, msg=f"Incorrect {i}-th probability")
def test_pauli_conversion_unitary(self): """Test conversion of Pauli channel kraus to unitary qobj""" error = QuantumError(self.depol_error(1), standard_gates=False) for j in range(4): instr, _ = error.error_term(j) self.assertEqual(len(instr), 1) self.assertIn(instr[0]['name'], ['unitary', 'id']) self.assertEqual(instr[0]['qubits'], [0]) target = SuperOp(Kraus(self.depol_error(1))) self.assertEqual(target, SuperOp(error))
def test_dot_unitary_and_kraus(self): """Test dot of a unitary and kraus error.""" kraus = self.kraus_error(0.4) unitaries = self.depol_error(0.1) error = QuantumError(unitaries).dot(QuantumError(kraus)) target = SuperOp(Kraus(unitaries)).dot(Kraus(kraus)) circ, prob = error.error_term(0) self.assertEqual(prob, 1) self.assertEqual(circ[0]['name'], 'kraus') self.assertEqual(circ[0]['qubits'], [0]) self.assertEqual(target, SuperOp(error))
def test_dot_both_kraus(self): """Test dot of two kraus errors""" kraus0 = self.kraus_error(0.3) kraus1 = self.kraus_error(0.5) error = QuantumError(kraus0).dot(QuantumError(kraus1)) target = SuperOp(Kraus(kraus0)).dot(Kraus(kraus1)) kraus, prob = error.error_term(0) self.assertEqual(prob, 1) self.assertEqual(kraus[0]['name'], 'kraus') self.assertEqual(kraus[0]['qubits'], [0]) self.assertEqual(target, SuperOp(error), msg="Incorrect dot kraus")
def test_compose_kraus_and_unitary(self): """Test compose of a kraus and unitary error.""" kraus = self.kraus_error(0.4) unitaries = self.depol_error(0.1) error = QuantumError(kraus).compose(QuantumError(unitaries)) target = SuperOp(Kraus(kraus)).compose(Kraus(unitaries)) circ, prob = error.error_term(0) self.assertEqual(prob, 1) self.assertEqual(circ[0]['name'], 'kraus') self.assertEqual(circ[0]['qubits'], [0]) self.assertEqual(target, SuperOp(error))
def test_expand_both_kraus(self): """Test expand of two kraus errors""" kraus0 = self.kraus_error(0.3) kraus1 = self.kraus_error(0.5) error = QuantumError(kraus0).expand(QuantumError(kraus1)) target = SuperOp(Kraus(kraus0)).expand(Kraus(kraus1)) circ, prob = error.error_term(0) self.assertEqual(prob, 1) self.assertEqual(circ[0]['name'], 'kraus') self.assertEqual(circ[0]['qubits'], [0, 1]) self.assertEqual(target, SuperOp(error))
def test_pauli_error_1q_gate_from_string(self): """Test single-qubit pauli error as gate qobj from string label""" paulis = ['I', 'X', 'Y', 'Z'] probs = [0.4, 0.3, 0.2, 0.1] actual = pauli_error(zip(paulis, probs)) expected = QuantumError([(IGate(), 0.4), (XGate(), 0.3), (YGate(), 0.2), (ZGate(), 0.1)]) for i in range(actual.size): circ, prob = actual.error_term(i) expected_circ, expected_prob = expected.error_term(i) self.assertEqual(circ, expected_circ, msg=f"Incorrect {i}-th circuit") self.assertAlmostEqual(prob, expected_prob, msg=f"Incorrect {i}-th probability")
def test_thermal_relaxation_error_t1_equal_t2_1state(self): """Test qobj instructions return for t1=t2""" actual = thermal_relaxation_error(1, 1, 1, 1) expected = QuantumError([ (IGate(), np.exp(-1)), ([(Reset(), [0]), (XGate(), [0])], 1 - np.exp(-1)), ]) for i in range(actual.size): circ, prob = actual.error_term(i) expected_circ, expected_prob = expected.error_term(i) self.assertEqual(circ, expected_circ, msg=f"Incorrect {i}-th circuit") self.assertAlmostEqual(prob, expected_prob, msg=f"Incorrect {i}-th probability")
def test_pauli_error_2q_gate_from_pauli(self): """Test two-qubit pauli error as gate qobj from Pauli obj""" paulis = [qi.Pauli(s) for s in ['XZ', 'YX', 'ZY']] probs = [0.5, 0.3, 0.2] actual = pauli_error(zip(paulis, probs)) expected = QuantumError( [(PauliGate("XZ"), 0.5), (PauliGate("YX"), 0.3), (PauliGate("ZY"), 0.2)] ) for i in range(actual.size): circ, prob = actual.error_term(i) expected_circ, expected_prob = expected.error_term(i) self.assertEqual(circ, expected_circ, msg=f"Incorrect {i}-th circuit") self.assertAlmostEqual(prob, expected_prob, msg=f"Incorrect {i}-th probability")
def test_depolarizing_error_1q_gate(self): """Test 1-qubit depolarizing error as gate qobj""" p_depol = 0.3 actual = depolarizing_error(p_depol, 1) expected = QuantumError([ (IGate(), 1 - p_depol*3/4), (XGate(), p_depol/4), (YGate(), p_depol/4), (ZGate(), p_depol/4) ]) for i in range(actual.size): circ, prob = actual.error_term(i) expected_circ, expected_prob = expected.error_term(i) self.assertEqual(circ, expected_circ, msg=f"Incorrect {i}-th circuit") self.assertAlmostEqual(prob, expected_prob, msg=f"Incorrect {i}-th probability")
def test_pauli_error_2q_gate_from_pauli(self): """Test two-qubit pauli error as gate qobj from Pauli obj""" paulis = [qi.Pauli(s) for s in ['XZ', 'YX', 'ZY']] probs = [0.5, 0.3, 0.2] with self.assertWarns(DeprecationWarning): actual = pauli_error(zip(paulis, probs), standard_gates=True) target_circs = [[{"name": "z", "qubits": [0]}, {"name": "x", "qubits": [1]}], [{"name": "x", "qubits": [0]}, {"name": "y", "qubits": [1]}], [{"name": "y", "qubits": [0]}, {"name": "z", "qubits": [1]}]] with self.assertWarns(DeprecationWarning): expected = QuantumError(zip(target_circs, probs), standard_gates=True) for i in range(actual.size): circ, prob = actual.error_term(i) expected_circ, expected_prob = expected.error_term(i) self.assertEqual(circ, expected_circ) self.assertAlmostEqual(prob, expected_prob)
def test_pauli_error_2q_unitary_from_pauli(self): """Test two-qubit pauli error as unitary qobj from Pauli obj""" from qiskit.extensions import UnitaryGate paulis = [qi.Pauli(s) for s in ['XY', 'YZ', 'ZX']] probs = [0.5, 0.3, 0.2] with self.assertWarns(DeprecationWarning): actual = pauli_error(zip(paulis, probs), standard_gates=False) X = qi.Pauli("X").to_matrix() Y = qi.Pauli("Y").to_matrix() Z = qi.Pauli("Z").to_matrix() target_unitaries = [np.kron(X, Y), np.kron(Y, Z), np.kron(Z, X)] expected = QuantumError([(UnitaryGate(mat), p) for mat, p in zip(target_unitaries, probs)]) for i in range(actual.size): circ, prob = actual.error_term(i) expected_circ, expected_prob = expected.error_term(i) self.assertEqual(circ, expected_circ) self.assertAlmostEqual(prob, expected_prob)
def test_pauli_error_1q_unitary_from_pauli(self): """Test single-qubit pauli error as unitary qobj from Pauli obj""" from qiskit.extensions import UnitaryGate paulis = [qi.Pauli(s) for s in ['I', 'X', 'Y', 'Z']] probs = [0.4, 0.3, 0.2, 0.1] with self.assertWarns(DeprecationWarning): actual = pauli_error(zip(paulis, probs), standard_gates=False) target_unitaries = [qi.Pauli("I").to_matrix(), qi.Pauli("X").to_matrix(), qi.Pauli("Y").to_matrix(), qi.Pauli("Z").to_matrix()] expected = QuantumError([(UnitaryGate(mat), p) for mat, p in zip(target_unitaries, probs)]) for i in range(actual.size): circ, prob = actual.error_term(i) expected_circ, expected_prob = expected.error_term(i) self.assertEqual(circ, expected_circ) self.assertAlmostEqual(prob, expected_prob)
def test_pauli_error_2q_gate_from_string_1qonly(self): """Test two-qubit pauli error as gate qobj from string label""" paulis = ['XI', 'YI', 'ZI'] probs = [0.5, 0.3, 0.2] with self.assertWarns(DeprecationWarning): actual = pauli_error(zip(paulis, probs), standard_gates=True) target_circs = [[{"name": "id", "qubits": [0]}, {"name": "x", "qubits": [1]}], [{"name": "id", "qubits": [0]}, {"name": "y", "qubits": [1]}], [{"name": "id", "qubits": [0]}, {"name": "z", "qubits": [1]}]] with self.assertWarns(DeprecationWarning): expected = QuantumError(zip(target_circs, probs), standard_gates=True) for i in range(actual.size): circ, prob = actual.error_term(i) expected_circ, expected_prob = expected.error_term(i) self.assertTrue(qi.Operator(circ).equiv(qi.Operator(expected_circ))) self.assertAlmostEqual(prob, expected_prob)
def test_thermal_relaxation_error_gate(self): """Test qobj instructions return for t2 < t1""" t1, t2, time, p1 = (2, 1, 1, 0.3) actual = thermal_relaxation_error(t1, t2, time, p1) p_z = 0.5 * np.exp(-1 / t1) * (1 - np.exp(-(1 / t2 - 1 / t1) * time)) p_reset0 = (1 - p1) * (1 - np.exp(-1 / t1)) p_reset1 = p1 * (1 - np.exp(-1 / t1)) expected = QuantumError([ (IGate(), 1 - p_z - p_reset0 - p_reset1), (ZGate(), p_z), (Reset(), p_reset0), ([(Reset(), [0]), (XGate(), [0])], p_reset1), ]) for i in range(actual.size): circ, prob = actual.error_term(i) expected_circ, expected_prob = expected.error_term(i) self.assertEqual(circ, expected_circ, msg=f"Incorrect {i}-th circuit") self.assertAlmostEqual(prob, expected_prob, msg=f"Incorrect {i}-th probability")