| 1686 | } |
| 1687 | |
| 1688 | void QVM::get_expectation_vector(QProg prog, const QHamiltonian& hamiltonian, const QVec& qv, vector_d& measure_probably) |
| 1689 | { |
| 1690 | directlyRun(prog); |
| 1691 | auto qstate = getQState(); |
| 1692 | initState(qstate); |
| 1693 | |
| 1694 | auto _parity_check = [](size_t number) |
| 1695 | { |
| 1696 | bool label = true; |
| 1697 | size_t i = 0; |
| 1698 | while ((number >> i) != 0) |
| 1699 | { |
| 1700 | if ((number >> i) % 2 == 1) |
| 1701 | label = !label; |
| 1702 | |
| 1703 | ++i; |
| 1704 | } |
| 1705 | return label; |
| 1706 | }; |
| 1707 | |
| 1708 | |
| 1709 | for (size_t i = 0; i < hamiltonian.size(); i++) |
| 1710 | { |
| 1711 | auto component = hamiltonian[i]; |
| 1712 | if (component.first.empty()) |
| 1713 | { |
| 1714 | measure_probably[i] = 1.0; |
| 1715 | continue; |
| 1716 | } |
| 1717 | |
| 1718 | QProg qprog; |
| 1719 | Qnum vqubit; |
| 1720 | for (auto iter : component.first) |
| 1721 | { |
| 1722 | vqubit.push_back(qv[iter.first]->get_phy_addr()); |
| 1723 | if (iter.second == 'X') |
| 1724 | qprog << H(qv[iter.first]); |
| 1725 | else if (iter.second == 'Y') |
| 1726 | qprog << RX(qv[iter.first], PI / 2); |
| 1727 | } |
| 1728 | |
| 1729 | directlyRun(qprog); |
| 1730 | prob_vec pmeasure_vector; |
| 1731 | _pGates->pMeasure(vqubit, pmeasure_vector); |
| 1732 | |
| 1733 | double expectation = 0; |
| 1734 | #pragma omp parallel for reduction(+:expectation) |
| 1735 | for (auto i = 0; i < pmeasure_vector.size(); i++) |
| 1736 | { |
| 1737 | if (_parity_check(i)) |
| 1738 | { |
| 1739 | expectation += pmeasure_vector[i]; |
| 1740 | } |
| 1741 | else |
| 1742 | { |
| 1743 | expectation -= pmeasure_vector[i]; |
| 1744 | } |
| 1745 | } |
nothing calls this directly
no test coverage detected