本文整理汇总了Python中pyquil.quil.Program方法的典型用法代码示例。如果您正苦于以下问题:Python quil.Program方法的具体用法?Python quil.Program怎么用?Python quil.Program使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyquil.quil
的用法示例。
在下文中一共展示了quil.Program方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _defgate
# 需要导入模块: from pyquil import quil [as 别名]
# 或者: from pyquil.quil import Program [as 别名]
def _defgate(self, program, gate_name, gate_matrix):
"""Defines a gate named gate_name with matrix gate_matrix in program. In addition, updates
self.defined_gates to track what has been defined.
:param Program program: Pyquil Program to add the defgate and gate to.
:param str gate_name: The name of the gate to add to program.
:param numpy.ndarray gate_matrix: The array corresponding to the gate to define.
:return: the modified Program.
:retype: Program
"""
new_program = pq.Program()
new_program += program
if gate_name not in self.defined_gates:
new_program.defgate(gate_name, gate_matrix)
self.defined_gates.add(gate_name)
return new_program
示例2: state_tomography_programs
# 需要导入模块: from pyquil import quil [as 别名]
# 或者: from pyquil.quil import Program [as 别名]
def state_tomography_programs(state_prep, qubits=None,
rotation_generator=tomography.default_rotations):
"""
Yield tomographic sequences that prepare a state with Quil program `state_prep` and then append
tomographic rotations on the specified `qubits`. If `qubits is None`, it assumes all qubits in
the program should be tomographically rotated.
:param Program state_prep: The program to prepare the state to be tomographed.
:param list|NoneType qubits: A list of Qubits or Numbers, to perform the tomography on. If
`None`, performs it on all in state_prep.
:param generator rotation_generator: A generator that yields tomography rotations to perform.
:return: Program for state tomography.
:rtype: Program
"""
if qubits is None:
qubits = state_prep.get_qubits()
for tomography_program in rotation_generator(*qubits):
state_tomography_program = Program(Pragma("PRESERVE_BLOCK"))
state_tomography_program.inst(state_prep)
state_tomography_program.inst(tomography_program)
state_tomography_program.inst(Pragma("END_PRESERVE_BLOCK"))
yield state_tomography_program
示例3: do_state_tomography
# 需要导入模块: from pyquil import quil [as 别名]
# 或者: from pyquil.quil import Program [as 别名]
def do_state_tomography(preparation_program, nsamples, cxn, qubits=None, use_run=False):
"""
Method to perform both a QPU and QVM state tomography, and use the latter as
as reference to calculate the fidelity of the former.
:param Program preparation_program: Program to execute.
:param int nsamples: Number of samples to take for the program.
:param QVMConnection|QPUConnection cxn: Connection on which to run the program.
:param list qubits: List of qubits for the program.
to use in the tomography analysis.
:param bool use_run: If ``True``, use append measurements on all qubits and use ``cxn.run``
instead of ``cxn.run_and_measure``.
:return: The state tomogram.
:rtype: StateTomography
"""
return tomography._do_tomography(preparation_program, nsamples, cxn, qubits,
tomography.MAX_QUBITS_STATE_TOMO,
StateTomography, state_tomography_programs,
DEFAULT_STATE_TOMO_SETTINGS, use_run=use_run)
示例4: basis_state_preps
# 需要导入模块: from pyquil import quil [as 别名]
# 或者: from pyquil.quil import Program [as 别名]
def basis_state_preps(*qubits):
"""
Generate a sequence of programs that prepares the measurement
basis states of some set of qubits in the order such that the qubit
with highest index is iterated over the most quickly:
E.g., for ``qubits=(0, 1)``, it returns the circuits::
I_0 I_1
I_0 X_1
X_0 I_1
X_0 X_1
:param list qubits: Each qubit to include in the basis state preparation.
:return: Yields programs for each basis state preparation.
:rtype: Program
"""
for prep in cartesian_product([I, X], repeat=len(qubits)):
basis_prep = Program(Pragma("PRESERVE_BLOCK"))
for gate, qubit in zip(prep, qubits):
basis_prep.inst(gate(qubit))
basis_prep.inst(Pragma("END_PRESERVE_BLOCK"))
yield basis_prep
示例5: density
# 需要导入模块: from pyquil import quil [as 别名]
# 或者: from pyquil.quil import Program [as 别名]
def density(self, pyquil_program):
"""
Run program and compute the density matrix
Loads and checks program if all gates are within the stabilizer set.
Then executes program and returns the final density matrix for the
stabilizer
:param Program pyquil_program: a pyquil Program containing only
CNOT-H-S-MEASUREMENT operations
:return:
"""
self.load_program(pyquil_program)
self.tableau = self._n_qubit_tableau(self.num_qubits)
self.kernel()
stabilizers = binary_stabilizer_to_pauli_stabilizer(self.stabilizer_tableau())
pauli_ops = reduce(lambda x, y: x * y, [0.5 * (sI(0) + b) for b in stabilizers])
return tensor_up(pauli_ops, self.num_qubits)
示例6: get_compiled_prog
# 需要导入模块: from pyquil import quil [as 别名]
# 或者: from pyquil.quil import Program [as 别名]
def get_compiled_prog(theta):
return Program([
RZgate(-pi/2, 0),
RXgate(-pi/2, 0),
RZgate(-pi/2, 1),
RXgate( pi/2, 1),
CZgate(1, 0),
RZgate(-pi/2, 1),
RXgate(-pi/2, 1),
RZgate(theta, 1),
RXgate( pi/2, 1),
CZgate(1, 0),
RXgate( pi/2, 0),
RZgate( pi/2, 0),
RZgate(-pi/2, 1),
RXgate( pi/2, 1),
RZgate(-pi/2, 1),
])
示例7: test_exp_circuit
# 需要导入模块: from pyquil import quil [as 别名]
# 或者: from pyquil.quil import Program [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))
示例8: test_errors
# 需要导入模块: from pyquil import quil [as 别名]
# 或者: from pyquil.quil import Program [as 别名]
def test_errors(qvm):
# NOP unsupported
prog = Program(NOP)
with pytest.raises(TypeError):
qvm.wavefunction(prog)
with pytest.raises(TypeError):
qvm.run(prog)
with pytest.raises(TypeError):
qvm.run_and_measure(prog)
# WAIT unsupported
prog = Program(WAIT)
with pytest.raises(TypeError):
qvm.wavefunction(prog)
with pytest.raises(TypeError):
qvm.run(prog)
with pytest.raises(TypeError):
qvm.run_and_measure(prog)
示例9: tests_against_cloud
# 需要导入模块: from pyquil import quil [as 别名]
# 或者: from pyquil.quil import Program [as 别名]
def tests_against_cloud(qvm):
# simple program
p = Program(H(0))
cloud_results = [[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]
local_results = qvm.run(p, classical_addresses=[0], trials=10)
assert len(cloud_results) == len(local_results)
cloud_wf = dc.HADAMARD_WF
local_wf, _ = qvm.wavefunction(p)
assert np.allclose(cloud_wf, local_wf.amplitudes)
# complex program
p = Program(dc.QFT_8_INSTRUCTIONS)
cloud_wf = dc.QFT_8_WF_PROBS
local_wf, _ = qvm.wavefunction(p)
assert np.allclose(cloud_wf, local_wf.amplitudes)
示例10: test_random_gates
# 需要导入模块: from pyquil import quil [as 别名]
# 或者: from pyquil.quil import Program [as 别名]
def test_random_gates(qvm_unitary):
p = Program().inst([H(0), H(1), H(0)])
test_unitary = qvm_unitary.unitary(p)
actual_unitary = np.kron(gate_matrix['H'], np.eye(2 ** 1))
assert np.allclose(test_unitary, actual_unitary)
p = Program().inst([H(0), X(1), Y(2), Z(3)])
test_unitary = qvm_unitary.unitary(p)
actual_unitary = np.kron(gate_matrix['Z'],
np.kron(gate_matrix['Y'],
np.kron(gate_matrix['X'],
gate_matrix['H'])))
assert np.allclose(test_unitary, actual_unitary)
p = Program().inst([X(2), CNOT(2, 1), CNOT(1, 0)])
test_unitary = qvm_unitary.unitary(p)
# gates are multiplied in 'backwards' order
actual_unitary = np.kron(np.eye(2 ** 1), gate_matrix['CNOT']).dot(
np.kron(gate_matrix['CNOT'], np.eye(2 ** 1))).dot(
np.kron(gate_matrix['X'], np.eye(2 ** 2)))
assert np.allclose(test_unitary, actual_unitary)
示例11: test_qaoa_unitary
# 需要导入模块: from pyquil import quil [as 别名]
# 或者: from pyquil.quil import Program [as 别名]
def test_qaoa_unitary(qvm_unitary):
wf_true = [0.00167784 + 1.00210180e-05*1j, 0.50000000 - 4.99997185e-01*1j,
0.50000000 - 4.99997185e-01*1j, 0.00167784 + 1.00210180e-05*1j]
prog = Program()
prog.inst([RY(np.pi/2, 0), RX(np.pi, 0),
RY(np.pi/2, 1), RX(np.pi, 1),
CNOT(0, 1), RX(-np.pi/2, 1), RY(4.71572463191, 1),
RX(np.pi/2, 1), CNOT(0, 1),
RX(-2*2.74973750579, 0), RX(-2*2.74973750579, 1)])
test_unitary = qvm_unitary.unitary(prog)
wf_test = np.zeros(4)
wf_test[0] = 1.0
wf_test = test_unitary.dot(wf_test)
assert np.allclose(wf_test, wf_true)
示例12: test_tensor_gates_single_qubit
# 需要导入模块: from pyquil import quil [as 别名]
# 或者: from pyquil.quil import Program [as 别名]
def test_tensor_gates_single_qubit():
prog = Program().inst([Hgate(0)])
test_unitary = tensor_gates(gate_matrix, {}, prog.instructions[0], 1).toarray()
true_unitary = gate_matrix['H']
assert np.allclose(test_unitary, true_unitary)
prog = Program().inst([Hgate(0)])
test_unitary = tensor_gates(gate_matrix, {}, prog.instructions[0], 5).toarray()
true_unitary = np.kron(np.eye(2**4), gate_matrix['H'])
assert np.allclose(test_unitary, true_unitary)
prog = Program().inst([RXgate(0.2, 3)])
test_unitary = tensor_gates(gate_matrix, {}, prog.instructions[0], 5).toarray()
true_unitary = np.kron(np.eye(2**1), np.kron(gate_matrix['RX'](0.2), np.eye(2**3)))
assert np.allclose(test_unitary, true_unitary)
prog = Program().inst([RXgate(0.5, 4)])
test_unitary = tensor_gates(gate_matrix, {}, prog.instructions[0], 5).toarray()
true_unitary = np.kron(np.eye(2**0), np.kron(gate_matrix['RX'](0.5), np.eye(2**4)))
assert np.allclose(test_unitary, true_unitary)
示例13: test_simulation_phase
# 需要导入模块: from pyquil import quil [as 别名]
# 或者: from pyquil.quil import Program [as 别名]
def test_simulation_phase():
"""
Test if the Phase gate is applied correctly to the tableau
S|0> = |0>
S|+> = |R>
"""
prog = Program().inst(S(0))
qvmstab = QVM_Stabilizer(num_qubits=1)
qvmstab._apply_phase(prog.instructions[0])
true_stab = np.array([[1, 1, 0],
[0, 1, 0]])
assert np.allclose(true_stab, qvmstab.tableau)
prog = Program().inst([H(0), S(0)])
qvmstab = QVM_Stabilizer(num_qubits=1)
qvmstab._apply_hadamard(prog.instructions[0])
qvmstab._apply_phase(prog.instructions[1])
true_stab = np.array([[0, 1, 0],
[1, 1, 0]])
assert np.allclose(true_stab, qvmstab.tableau)
示例14: run
# 需要导入模块: from pyquil import quil [as 别名]
# 或者: from pyquil.quil import Program [as 别名]
def run(self, pyquil_program, classical_addresses=None, trials=1):
"""
Run a pyQuil program multiple times, accumulating the values deposited
in a list of classical addresses.
This uses numpy's inverse sampling method to calculate bit string
outcomes
:param Program pyquil_program: A pyQuil program.
:param list classical_addresses: A list of classical addresses.
This is ignored but kept to have
similar input as Forest QVM.
:param int trials: Number of shots to collect.
:return: A list of lists of bits. Each sublist corresponds to the
values in `classical_addresses`.
:rtype: list
"""
results = []
for trial in range(trials):
_ = self.density(pyquil_program)
results.append(self.classical_memory[classical_addresses])
return list(results)
示例15: run
# 需要导入模块: from pyquil import quil [as 别名]
# 或者: from pyquil.quil import Program [as 别名]
def run(self, pyquil_program, classical_addresses=None, trials=1):
"""
Run a pyQuil program multiple times, accumulating the values deposited
in a list of classical addresses.
:param Program pyquil_program: A pyQuil program.
:param list classical_addresses: A list of classical addresses.
:param int trials: Number of shots to collect.
:return: A list of lists of bits. Each sublist corresponds to the
values in `classical_addresses`.
:rtype: list
"""
results = []
for trial in range(trials):
_, classical_vals = self.wavefunction(pyquil_program,
classical_addresses=classical_addresses)
results.append(classical_vals)
return results