def test_probabilities_dict_w(self): """Test probabilities_dict method with W state""" psi = ( Statevector.from_label("001") + Statevector.from_label("010") + Statevector.from_label("100") ) / np.sqrt(3) state = DensityMatrix(psi) # 3-qubit qargs target = np.array([0, 1 / 3, 1 / 3, 0, 1 / 3, 0, 0, 0]) target = {"001": 1 / 3, "010": 1 / 3, "100": 1 / 3} for qargs in [[0, 1, 2], [2, 1, 0], [1, 2, 0], [1, 0, 2]]: with self.subTest(msg=f"P({qargs})"): probs = state.probabilities_dict(qargs) self.assertDictAlmostEqual(probs, target) # 2-qubit qargs target = {"00": 1 / 3, "01": 1 / 3, "10": 1 / 3} for qargs in [[0, 1], [2, 1], [1, 2], [1, 2]]: with self.subTest(msg=f"P({qargs})"): probs = state.probabilities_dict(qargs) self.assertDictAlmostEqual(probs, target) # 1-qubit qargs target = {"0": 2 / 3, "1": 1 / 3} for qargs in [[0], [1], [2]]: with self.subTest(msg=f"P({qargs})"): probs = state.probabilities_dict(qargs) self.assertDictAlmostEqual(probs, target)
def test_probabilities_dict_w(self): """Test probabilities_dict method with W state""" psi = (Statevector.from_label('001') + Statevector.from_label('010') + Statevector.from_label('100')) / np.sqrt(3) state = DensityMatrix(psi) # 3-qubit qargs target = np.array([0, 1 / 3, 1 / 3, 0, 1 / 3, 0, 0, 0]) target = {'001': 1 / 3, '010': 1 / 3, '100': 1 / 3} for qargs in [[0, 1, 2], [2, 1, 0], [1, 2, 0], [1, 0, 2]]: with self.subTest(msg='P({})'.format(qargs)): probs = state.probabilities_dict(qargs) self.assertDictAlmostEqual(probs, target) # 2-qubit qargs target = {'00': 1 / 3, '01': 1 / 3, '10': 1 / 3} for qargs in [[0, 1], [2, 1], [1, 2], [1, 2]]: with self.subTest(msg='P({})'.format(qargs)): probs = state.probabilities_dict(qargs) self.assertDictAlmostEqual(probs, target) # 1-qubit qargs target = {'0': 2 / 3, '1': 1 / 3} for qargs in [[0], [1], [2]]: with self.subTest(msg='P({})'.format(qargs)): probs = state.probabilities_dict(qargs) self.assertDictAlmostEqual(probs, target)
def test_probabilities_dict_ghz(self): """Test probabilities_dict method for GHZ state""" psi = (Statevector.from_label("000") + Statevector.from_label("111")) / np.sqrt(2) state = DensityMatrix(psi) # 3-qubit qargs target = {"000": 0.5, "111": 0.5} for qargs in [[0, 1, 2], [2, 1, 0], [1, 2, 0], [1, 0, 2]]: with self.subTest(msg=f"P({qargs})"): probs = state.probabilities_dict(qargs) self.assertDictAlmostEqual(probs, target) # 2-qubit qargs target = {"00": 0.5, "11": 0.5} for qargs in [[0, 1], [2, 1], [1, 2], [1, 2]]: with self.subTest(msg=f"P({qargs})"): probs = state.probabilities_dict(qargs) self.assertDictAlmostEqual(probs, target) # 1-qubit qargs target = {"0": 0.5, "1": 0.5} for qargs in [[0], [1], [2]]: with self.subTest(msg=f"P({qargs})"): probs = state.probabilities_dict(qargs) self.assertDictAlmostEqual(probs, target)
def test_probabilities_dict_ghz(self): """Test probabilities_dict method for GHZ state""" psi = (Statevector.from_label('000') + Statevector.from_label('111')) / np.sqrt(2) state = DensityMatrix(psi) # 3-qubit qargs target = {'000': 0.5, '111': 0.5} for qargs in [[0, 1, 2], [2, 1, 0], [1, 2, 0], [1, 0, 2]]: with self.subTest(msg='P({})'.format(qargs)): probs = state.probabilities_dict(qargs) self.assertDictAlmostEqual(probs, target) # 2-qubit qargs target = {'00': 0.5, '11': 0.5} for qargs in [[0, 1], [2, 1], [1, 2], [1, 2]]: with self.subTest(msg='P({})'.format(qargs)): probs = state.probabilities_dict(qargs) self.assertDictAlmostEqual(probs, target) # 1-qubit qargs target = {'0': 0.5, '1': 0.5} for qargs in [[0], [1], [2]]: with self.subTest(msg='P({})'.format(qargs)): probs = state.probabilities_dict(qargs) self.assertDictAlmostEqual(probs, target)