當前位置: 首頁>>代碼示例>>Python>>正文


Python paulis.PauliTerm方法代碼示例

本文整理匯總了Python中pyquil.paulis.PauliTerm方法的典型用法代碼示例。如果您正苦於以下問題:Python paulis.PauliTerm方法的具體用法?Python paulis.PauliTerm怎麽用?Python paulis.PauliTerm使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pyquil.paulis的用法示例。


在下文中一共展示了paulis.PauliTerm方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: product_ops

# 需要導入模塊: from pyquil import paulis [as 別名]
# 或者: from pyquil.paulis import PauliTerm [as 別名]
def product_ops(self, indices, conjugate):
        """
        Convert a list of site indices and coefficients to a Pauli Operators
        list with the generalized Bravyi-Kitaev transformation.

        :param list indices: list of ints specifying the site the fermionic
                             operator acts on, e.g. [0,2,4,6]
        :param list conjugate: List of -1, 1 specifying which of the indices
                               are creation operators (-1) and which are
                               annihilation operators (1).  e.g. [-1,-1,1,1]
        """
        pterm = PauliTerm('I', 0, 1.0)
        for conj, index in zip(conjugate, indices):
            pterm = pterm * self._operator_generator(index, conj)

        pterm = pterm.simplify()
        return pterm 
開發者ID:rigetti,項目名稱:grove,代碼行數:19,代碼來源:bktransform.py

示例2: product_ops

# 需要導入模塊: from pyquil import paulis [as 別名]
# 或者: from pyquil.paulis import PauliTerm [as 別名]
def product_ops(self, indices, conjugate):
        """
        Convert a list of site indices and coefficients to a Pauli Operators
        list with the Jordan-Wigner (JW) transformation

        :param List indices: list of ints specifying the site the fermionic operator acts on,
                             e.g. [0,2,4,6]
        :param List conjugate: List of -1, 1 specifying which of the indices are
                               creation operators (-1) and which are annihilation
                               operators (1).  e.g. [-1,-1,1,1]
        """
        pterm = PauliTerm('I', 0, 1.0)
        for conj, index in zip(conjugate, indices):
            pterm = pterm * self._operator_generator(index, conj)

        pterm = pterm.simplify()
        return pterm 
開發者ID:rigetti,項目名稱:grove,代碼行數:19,代碼來源:jwtransform.py

示例3: _operator_generator

# 需要導入模塊: from pyquil import paulis [as 別名]
# 或者: from pyquil.paulis import PauliTerm [as 別名]
def _operator_generator(index, conj):
        """
        Internal method to generate the appropriate operator
        """
        pterm = PauliTerm('I', 0, 1.0)
        Zstring = PauliTerm('I', 0, 1.0)
        for j in range(index):
            Zstring = Zstring*PauliTerm('Z', j, 1.0)

        pterm1 = Zstring*PauliTerm('X', index, 0.5)
        scalar = 0.5 * conj * 1.0j
        pterm2 = Zstring*PauliTerm('Y', index, scalar)
        pterm = pterm * (pterm1 + pterm2)

        pterm = pterm.simplify()
        return pterm 
開發者ID:rigetti,項目名稱:grove,代碼行數:18,代碼來源:jwtransform.py

示例4: test_pass_hamiltonians

# 需要導入模塊: from pyquil import paulis [as 別名]
# 或者: from pyquil.paulis import PauliTerm [as 別名]
def test_pass_hamiltonians():
    ref_ham = [PauliSum([PauliTerm("X", 0, -1.0)]), PauliSum([PauliTerm("X", 1, -1.0)])]
    cost_ham = [PauliTerm("I", 0, 0.5) + PauliTerm("Z", 0, -0.5) * PauliTerm("Z", 1, 1.0)]
    fake_qc = Mock()
    inst = QAOA(fake_qc, list(range(2)), steps=1,
                cost_ham=cost_ham, ref_ham=ref_ham)

    c = inst.cost_ham
    r = inst.ref_ham
    assert isinstance(c, list)
    assert isinstance(r, list)
    assert isinstance(c[0], PauliSum)
    assert isinstance(r[0], PauliSum)
    assert len(c) == 1
    assert len(r) == 2

    with pytest.raises(TypeError):
        QAOA(fake_qc, 2, steps=1,
             cost_ham=PauliTerm("X", 0, 1.0), ref_ham=ref_ham,
             rand_seed=42) 
開發者ID:rigetti,項目名稱:grove,代碼行數:22,代碼來源:test_maxcut.py

示例5: test_PrepareAndMeasureOnWFSim

# 需要導入模塊: from pyquil import paulis [as 別名]
# 或者: from pyquil.paulis import PauliTerm [as 別名]
def test_PrepareAndMeasureOnWFSim():
    p = Program()
    params = p.declare("params", memory_type="REAL", memory_size=2)
    p.inst(RX(params[0], 0))
    p.inst(RX(params[1], 1))

    def make_memory_map(params):
        return {"params": params}

#    ham = PauliSum.from_compact_str("1.0*Z0 + 1.0*Z1")
    term1 = PauliTerm("Z", 0)
    term2 = PauliTerm("Z", 1)
    ham = PauliSum([term1, term2])
    sim = WavefunctionSimulator()
    cost_fn = PrepareAndMeasureOnWFSim(p,
                                       make_memory_map,
                                       ham,
                                       sim,
                                       scalar_cost_function=False,
                                       enable_logging=True)
    out = cost_fn([np.pi, np.pi / 2])
    print(cost_fn.log[0].fun)
    assert np.allclose(cost_fn.log[0].fun, (-1.0, 0.0))
    assert np.allclose(out, (-1, 0.0)) 
開發者ID:entropicalabs,項目名稱:entropica_qaoa,代碼行數:26,代碼來源:test_cost_function.py

示例6: test_PrepareAndMeasureOnWFSim_QubitPlaceholders

# 需要導入模塊: from pyquil import paulis [as 別名]
# 或者: from pyquil.paulis import PauliTerm [as 別名]
def test_PrepareAndMeasureOnWFSim_QubitPlaceholders():
    q1, q2 = QubitPlaceholder(), QubitPlaceholder()
    p = Program()
    params = p.declare("params", memory_type="REAL", memory_size=2)
    p.inst(RX(params[0], q1))
    p.inst(RX(params[1], q2))

    def make_memory_map(params):
        return {"params": params}

    ham = PauliSum([PauliTerm("Z", q1), PauliTerm("Z", q2)])
    qubit_mapping = get_default_qubit_mapping(p)
    sim = WavefunctionSimulator()
    cost_fn = PrepareAndMeasureOnWFSim(p, make_memory_map, ham, sim,
                                       enable_logging=True,
                                       qubit_mapping=qubit_mapping,
                                       scalar_cost_function=False,
                                       )
    out = cost_fn([np.pi, np.pi / 2])
    assert np.allclose(cost_fn.log[0].fun, (-1.0, 0.0))
    assert np.allclose(out, (-1, 0.0)) 
開發者ID:entropicalabs,項目名稱:entropica_qaoa,代碼行數:23,代碼來源:test_cost_function.py

示例7: test_PrepareAndMeasureOnQVM

# 需要導入模塊: from pyquil import paulis [as 別名]
# 或者: from pyquil.paulis import PauliTerm [as 別名]
def test_PrepareAndMeasureOnQVM():
    prepare_ansatz = Program()
    param_register = prepare_ansatz.declare(
        "params", memory_type="REAL", memory_size=2)
    prepare_ansatz.inst(RX(param_register[0], 0))
    prepare_ansatz.inst(RX(param_register[1], 1))

    def make_memory_map(params):
        return {"params": params}

#    ham = PauliSum.from_compact_str("1.0*Z0 + 1.0*Z1")
    term1 = PauliTerm("Z", 0)
    term2 = PauliTerm("Z", 1)
    ham = PauliSum([term1, term2])
    qvm = get_qc("2q-qvm")
    cost_fn = PrepareAndMeasureOnQVM(prepare_ansatz, make_memory_map, qvm=qvm,
                                     hamiltonian=ham, enable_logging=True,
                                     scalar_cost_function=True,
                                     base_numshots=10,
                                     nshots=10)
    out = cost_fn([np.pi, np.pi / 2])
    assert np.allclose(cost_fn.log[0].fun, (-1.0, 0.1), rtol=1.1)
    assert np.allclose(out, -1, rtol=1.1) 
開發者ID:entropicalabs,項目名稱:entropica_qaoa,代碼行數:25,代碼來源:test_cost_function.py

示例8: test_PrepareAndMeasureOnQVM_QubitPlaceholders

# 需要導入模塊: from pyquil import paulis [as 別名]
# 或者: from pyquil.paulis import PauliTerm [as 別名]
def test_PrepareAndMeasureOnQVM_QubitPlaceholders():
    q1, q2 = QubitPlaceholder(), QubitPlaceholder()
    prepare_ansatz = Program()
    param_register = prepare_ansatz.declare(
        "params", memory_type="REAL", memory_size=2)
    prepare_ansatz.inst(RX(param_register[0], q1))
    prepare_ansatz.inst(RX(param_register[1], q2))

    def make_memory_map(params):
        return {"params": params}

    ham = PauliSum([PauliTerm("Z", q1), PauliTerm("Z",q2)])
    qubit_mapping = get_default_qubit_mapping(prepare_ansatz)
    qvm = get_qc("2q-qvm")
    cost_fn = PrepareAndMeasureOnQVM(prepare_ansatz, make_memory_map,
                                     qvm=qvm,
                                     hamiltonian=ham, enable_logging=True,
                                     scalar_cost_function=False,
                                     base_numshots=10,
                                     qubit_mapping=qubit_mapping)
    out = cost_fn([np.pi, np.pi / 2], nshots=10)
    assert np.allclose(cost_fn.log[0].fun, (-1.0, 0.1), rtol=1.1)
    assert np.allclose(out, (-1, 0.1), rtol=1.1) 
開發者ID:entropicalabs,項目名稱:entropica_qaoa,代碼行數:25,代碼來源:test_cost_function.py

示例9: test_sampling_expectation

# 需要導入模塊: from pyquil import paulis [as 別名]
# 或者: from pyquil.paulis import PauliTerm [as 別名]
def test_sampling_expectation():
    bitstring1 = np.array([[1, 0], [0, 1], [1, 1], [1, 1]])
    bitstring2 = np.array([[1, 0], [0, 1], [1, 1], [1, 1]])
    bitstrings = [bitstring1, bitstring2]
    # ham1 = PauliSum.from_compact_str("1.0*Z0*Z1 + 0.5*Z0 + (-1)*Z1")
    # ham2 = PauliSum.from_compact_str("1.0*X0*Z1 + 0.5*X0 + (-1)*Z1")
    term1 = PauliTerm("Z", 0) * PauliTerm("Z", 1)
    term2 = PauliTerm("Z", 0, 0.5)
    term3 = PauliTerm("Z", 1, -1)
    term4 = PauliTerm("X", 0) * PauliTerm("Z", 1)
    term5 = PauliTerm("X", 0, 0.5)
    ham1 = PauliSum([term1, term2, term3])
    ham2 = PauliSum([term4, term5, term3])
    hams = [ham1, ham2]
    out = sampling_expectation(hams, bitstrings)
    assert np.allclose(out, (0.5, 1.3385315336840842)) 
開發者ID:entropicalabs,項目名稱:entropica_qaoa,代碼行數:18,代碼來源:test_measurelib.py

示例10: test_wavefunction_expectation

# 需要導入模塊: from pyquil import paulis [as 別名]
# 或者: from pyquil.paulis import PauliTerm [as 別名]
def test_wavefunction_expectation():
    term1 = PauliTerm("Z", 1, .2) * PauliTerm("Z", 2)
    term2 = PauliTerm("X", 1, -.4) * PauliTerm("Z", 3)
    term3 = PauliTerm("Y", 1, 1.4)
    ham = PauliSum([term1, term2, term3])
    ham2 = ham * ham

    mat = lifted_pauli(ham, [1, 3, 2])
    mat2 = lifted_pauli(ham2, [1, 3, 2])
    wf = np.array([0, 1.0, 2, .3, .5, -.5, .6, -.9])

    hams = commuting_decomposition(ham)
    hams2 = commuting_decomposition(ham2)

    funs = [base_change_fun(ham, [1, 3, 2]) for ham in hams]
    funs2 = [base_change_fun(ham, [1, 3, 2]) for ham in hams2]

    hams = [kron_eigs(h, [1, 3, 2]) for h in hams]
    hams2 = [kron_eigs(h, [1, 3, 2]) for h in hams2]

    e1, s1 = wavefunction_expectation(hams, funs, hams2, funs2, wf)
    e2, s2 = wf.conj()@mat@wf, wf@mat2@wf
    print(e1, s1)
    print(e2, s2)
    assert np.allclose((e1, s1), (e2, s2)) 
開發者ID:entropicalabs,項目名稱:entropica_qaoa,代碼行數:27,代碼來源:test_measurelib.py

示例11: test_qaoa_on_wfsim

# 需要導入模塊: from pyquil import paulis [as 別名]
# 或者: from pyquil.paulis import PauliTerm [as 別名]
def test_qaoa_on_wfsim():
    # ham = PauliSum.from_compact_str("(-1.0)*Z0*Z1 + 0.8*Z0 + (-0.5)*Z1")
    term1 = PauliTerm("Z", 0, -1) * PauliTerm("Z", 1)
    term2 = PauliTerm("Z", 0, 0.8)
    term3 = PauliTerm("Z", 1, -0.5)
    ham = PauliSum([term1, term2, term3])

    params = FourierWithBiasParams.linear_ramp_from_hamiltonian(ham,
                                                                n_steps=10,
                                                                q=2)
    p0 = params.raw()
    cost_fun = QAOACostFunctionOnWFSim(ham, params,
                                       scalar_cost_function=True)
    out = minimize(cost_fun, p0, tol=1e-3, method="Cobyla",
                   options={"maxiter": 500})
    wf = cost_fun.get_wavefunction(params.raw())
    assert np.allclose(out["fun"], -1.3, rtol=1.1)
    assert out["success"]
    assert np.allclose(wf.probabilities(), [0, 0, 0, 1], rtol=1.5, atol=0.05) 
開發者ID:entropicalabs,項目名稱:entropica_qaoa,代碼行數:21,代碼來源:test_qaoa.py

示例12: test_qaoa_on_qvm

# 需要導入模塊: from pyquil import paulis [as 別名]
# 或者: from pyquil.paulis import PauliTerm [as 別名]
def test_qaoa_on_qvm():
    # ham = PauliSum.from_compact_str("(-1.0)*Z0*Z1 + 0.8*Z0 + (-0.5)*Z1")
    term1 = PauliTerm("Z", 0, -1) * PauliTerm("Z", 1)
    term2 = PauliTerm("Z", 0, 0.8)
    term3 = PauliTerm("Z", 1, -0.5)
    ham = PauliSum([term1, term2, term3])
    params = FourierParams.linear_ramp_from_hamiltonian(ham,
                                                        n_steps=10,
                                                        q=2)
    p0 = params.raw()
    cost_fun = QAOACostFunctionOnQVM(ham, params, "2q-qvm",
                                     scalar_cost_function=True, nshots=4,
                                     base_numshots=50)
    out = minimize(cost_fun, p0, tol=2e-1, method="Cobyla",
                   options={"maxiter": 100})
    assert np.allclose(out["fun"], -1.3, rtol=1.1)
    assert out["success"] 
開發者ID:entropicalabs,項目名稱:entropica_qaoa,代碼行數:19,代碼來源:test_qaoa.py

示例13: test_exp_circuit

# 需要導入模塊: from pyquil import paulis [as 別名]
# 或者: from pyquil.paulis import PauliTerm [as 別名]
def test_exp_circuit(qvm):
    true_wf = np.array([0.54030231-0.84147098j,
                        0.00000000+0.j,
                        0.00000000+0.j,
                        0.00000000+0.j,
                        0.00000000+0.j,
                        0.00000000+0.j,
                        0.00000000+0.j,
                        0.00000000+0.j])

    create2kill1 = PauliTerm("X", 1, -0.25)*PauliTerm("Y", 2)
    create2kill1 += PauliTerm("Y", 1, 0.25)*PauliTerm("Y", 2)
    create2kill1 += PauliTerm("Y", 1, 0.25)*PauliTerm("X", 2)
    create2kill1 += PauliTerm("X", 1, 0.25)*PauliTerm("X", 2)
    create2kill1 += PauliTerm("I", 0, 1.0)
    prog = Program()
    for term in create2kill1.terms:
        single_exp_prog = exponentiate(term)
        prog += single_exp_prog

    wf, _ = qvm.wavefunction(prog)
    wf = np.reshape(wf.amplitudes, -1)
    assert np.allclose(wf.dot(np.conj(wf).T), true_wf.dot(np.conj(true_wf).T)) 
開發者ID:rigetti,項目名稱:reference-qvm,代碼行數:25,代碼來源:test_wavefunction.py

示例14: test_sync_paulisum_expectation

# 需要導入模塊: from pyquil import paulis [as 別名]
# 或者: from pyquil.paulis import PauliTerm [as 別名]
def test_sync_paulisum_expectation(qvm: QVMConnection):
    mock_qvm = qvm
    mock_endpoint = mock_qvm.sync_endpoint

    def mock_response(request, context):
        assert json.loads(request.text) == {
            "type": "expectation",
            "state-preparation": BELL_STATE.out(),
            "operators": ["Z 0\nZ 1\n", "Z 0\n", "Z 1\n"],
            "rng-seed": 52,
        }
        return b"[1.0, 0.0, 0.0]"

    with requests_mock.Mocker() as m:
        m.post(mock_endpoint + "/qvm", content=mock_response)
        z0 = PauliTerm("Z", 0)
        z1 = PauliTerm("Z", 1)
        z01 = z0 * z1
        result = mock_qvm.pauli_expectation(BELL_STATE, 1j * z01 + z0 + z1)
        exp_expected = 1j
        np.testing.assert_allclose(exp_expected, result) 
開發者ID:rigetti,項目名稱:pyquil,代碼行數:23,代碼來源:test_api.py

示例15: pauli_term_to_preparation_memory_map

# 需要導入模塊: from pyquil import paulis [as 別名]
# 或者: from pyquil.paulis import PauliTerm [as 別名]
def pauli_term_to_preparation_memory_map(
    term: PauliTerm, label: str = "preparation"
) -> Dict[str, List[float]]:
    """
    Given a ``PauliTerm``, create a memory map corresponding to the ZXZXZ-decomposed single-qubit
    gates that prepare the plus one eigenstate of the ``PauliTerm``. For example, if we have the
    following program:

        RZ(preparation_alpha[0]) 0
        RX(pi/2) 0
        RZ(preparation_beta[0]) 0
        RX(-pi/2) 0
        RZ(preparation_gamma[0]) 0

    We can prepare the ``|+>`` state (by default we start in the ``|0>`` state) by providing the
    following memory map (which corresponds to ``RY(pi/2)``):

        {'preparation_alpha': [0.0], 'preparation_beta': [pi/2], 'preparation_gamma': [0.0]}

    :param term: The ``PauliTerm`` in question.
    :param label: The prefix to provide to ``pauli_term_to_euler_memory_map``, for labeling the
        declared memory regions. Defaults to "preparation".
    :return: Memory map for preparing the desired state.
    """
    return pauli_term_to_euler_memory_map(term, prefix=label, tuple_x=P_X, tuple_y=P_Y, tuple_z=P_Z) 
開發者ID:rigetti,項目名稱:pyquil,代碼行數:27,代碼來源:_memory.py


注:本文中的pyquil.paulis.PauliTerm方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。