本文整理汇总了Python中pyquil.gates.H属性的典型用法代码示例。如果您正苦于以下问题:Python gates.H属性的具体用法?Python gates.H怎么用?Python gates.H使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类pyquil.gates
的用法示例。
在下文中一共展示了gates.H属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: oracle_grover
# 需要导入模块: from pyquil import gates [as 别名]
# 或者: from pyquil.gates import H [as 别名]
def oracle_grover(oracle: Program, qubits: List[int], num_iter: int = None) -> Program:
"""
Implementation of Grover's Algorithm for a given oracle.
:param oracle: An oracle defined as a Program. It should send :math:`\ket{x}`
to :math:`(-1)^{f(x)}\ket{x}`, where the range of f is {0, 1}.
:param qubits: List of qubits for Grover's Algorithm.
:param num_iter: The number of iterations to repeat the algorithm for.
The default is the integer closest to :math:`\frac{\pi}{4}\sqrt{N}`,
where :math:`N` is the size of the domain.
:return: A program corresponding to the desired instance of Grover's Algorithm.
"""
if num_iter is None:
num_iter = int(round(np.pi * 2 ** (len(qubits) / 2.0 - 2.0)))
uniform_superimposer = Program().inst([H(qubit) for qubit in qubits])
amp_prog = amplification_circuit(uniform_superimposer, oracle, qubits, num_iter)
return amp_prog
示例2: _core_qft
# 需要导入模块: from pyquil import gates [as 别名]
# 或者: from pyquil.gates import H [as 别名]
def _core_qft(qubits: List[int], coeff: int) -> Program:
"""
Generates the core program to perform the quantum Fourier transform
:param qubits: A list of qubit indexes.
:param coeff: A modifier for the angle used in rotations (-1 for inverse QFT, 1 for QFT)
:return: A Quil program to compute the core (inverse) QFT of the qubits.
"""
q = qubits[0]
qs = qubits[1:]
if 1 == len(qubits):
return [H(q)]
else:
n = 1 + len(qs)
cR = []
for idx, i in enumerate(range(n - 1, 0, -1)):
q_idx = qs[idx]
angle = math.pi / 2 ** (n - i)
cR.append(CPHASE(coeff * angle, q, q_idx))
return _core_qft(qs, coeff) + list(reversed(cR)) + [H(q)]
示例3: _create_bv_circuit
# 需要导入模块: from pyquil import gates [as 别名]
# 或者: from pyquil.gates import H [as 别名]
def _create_bv_circuit(self, bit_map: Dict[str, str]) -> Program:
"""
Implementation of the Bernstein-Vazirani Algorithm.
Given a list of input qubits and an ancilla bit, all initially in the
:math:`\\vert 0\\rangle` state, create a program that can find :math:`\\vec{a}` with one
query to the given oracle.
:param Dict[String, String] bit_map: truth-table of a function for Bernstein-Vazirani with
the keys being all possible bit vectors strings and the values being the function values
:rtype: Program
"""
unitary, _ = self._compute_unitary_oracle_matrix(bit_map)
full_bv_circuit = Program()
full_bv_circuit.defgate("BV-ORACLE", unitary)
# Put ancilla bit into minus state
full_bv_circuit.inst(X(self.ancilla), H(self.ancilla))
full_bv_circuit.inst([H(i) for i in self.computational_qubits])
full_bv_circuit.inst(
tuple(["BV-ORACLE"] + sorted(self.computational_qubits + [self.ancilla], reverse=True)))
full_bv_circuit.inst([H(i) for i in self.computational_qubits])
return full_bv_circuit
示例4: test_diffusion_operator
# 需要导入模块: from pyquil import gates [as 别名]
# 或者: from pyquil.gates import H [as 别名]
def test_diffusion_operator():
"""
Checks that the diffusion operator outputs the correct operation
"""
created = decomposed_diffusion_program(qubits[:2])
desired = Program()
for def_gate in created.defined_gates:
desired.defgate(def_gate.name, def_gate.matrix)
qubit0 = qubits[0]
qubit1 = qubits[1]
desired.inst(X(qubit0))
desired.inst(X(qubit1))
desired.inst(H(qubit1))
desired.inst(RZ(-np.pi, qubit0))
desired.inst(CNOT(qubit0, qubit1))
desired.inst(RZ(-np.pi, qubit0))
desired.inst(H(qubit1))
desired.inst(X(qubit0))
desired.inst(X(qubit1))
assert desired == created
示例5: test_x_oracle_two_grover
# 需要导入模块: from pyquil import gates [as 别名]
# 或者: from pyquil.gates import H [as 别名]
def test_x_oracle_two_grover(x_oracle):
"""Testing that Grover's algorithm with an oracle that applies an X gate to the query bit works,
with two iterations."""
qubits = [0]
oracle, _ = x_oracle
generated_x_oracle_grover = Grover().oracle_grover(oracle, qubits, num_iter=2)
# First we put the input into uniform superposition.
gates = [H]
for _ in range(2):
# Now an oracle is applied.
gates.append(X)
# We apply the diffusion operator.
gates.append(H)
gates.append(HADAMARD_DIFFUSION_LABEL)
gates.append(H)
check_instructions(gates, generated_x_oracle_grover)
示例6: test_gradient_program
# 需要导入模块: from pyquil import gates [as 别名]
# 或者: from pyquil.gates import H [as 别名]
def test_gradient_program():
f_h = 0.25
precision = 2
trial_prog = gradient_program(f_h, precision)
result_prog = Program([H(0), H(1)])
phase_factor = np.exp(1.0j * 2 * np.pi * abs(f_h))
U = np.array([[phase_factor, 0],
[0, phase_factor]])
q_out = range(precision, precision+1)
for i in range(precision):
if i > 0:
U = np.dot(U, U)
cU = controlled(U)
name = "CONTROLLED-U{0}".format(2 ** i)
result_prog.defgate(name, cU)
result_prog.inst((name, i) + tuple(q_out))
result_prog.inst([SWAP(0, 1), H(0), CPHASE(-1.5707963267948966, 0, 1),
H(1), MEASURE(0, 0), MEASURE(1, 1)])
assert(trial_prog == result_prog)
示例7: test_run_swap
# 需要导入模块: from pyquil import gates [as 别名]
# 或者: from pyquil.gates import H [as 别名]
def test_run_swap():
"""
Test the qvm return piece
"""
expected_bitstring = [1, 1, 1, 0, 0, 0, 0, 0, 0]
prog_a = Program().inst(H(0))
prog_b = Program().inst(H(1))
with patch("pyquil.api.QuantumComputer") as qc:
qc.run.return_value = expected_bitstring
test_overlap = run_swap_test(prog_a, prog_b,
number_of_measurements=5,
quantum_resource=qc)
assert np.isclose(np.sqrt(1 - 2 * np.mean(expected_bitstring)),
test_overlap)
expected_bitstring = [1, 1, 1, 0, 1]
prog_a = Program().inst(H(0))
prog_b = Program().inst(H(1))
with patch("pyquil.api.QuantumComputer") as qc:
qc.run.return_value = expected_bitstring
with pytest.raises(ValueError):
test_overlap = run_swap_test(prog_a, prog_b,
number_of_measurements=5,
quantum_resource=qc)
示例8: test_tensor_gates_single_qubit
# 需要导入模块: from pyquil import gates [as 别名]
# 或者: from pyquil.gates import H [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)
示例9: test_simulation_phase
# 需要导入模块: from pyquil import gates [as 别名]
# 或者: from pyquil.gates import H [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)
示例10: test_parameter_not_given_error
# 需要导入模块: from pyquil import gates [as 别名]
# 或者: from pyquil.gates import H [as 别名]
def test_parameter_not_given_error(self):
"""Test that the correct error is raised if a parameter is not given."""
program = pyquil.Program()
alpha = program.declare("alpha", "REAL")
beta = program.declare("beta", "REAL")
program += g.H(0)
program += g.CNOT(0, 1)
program += g.RX(alpha, 1)
program += g.RZ(beta, 1)
a = 0.1
parameter_map = {"alpha": a}
with pytest.raises(
qml.DeviceError,
match="The PyQuil program defines a variable .* that is not present in the given variable map",
):
load_program(program)(wires=range(2), parameter_map=parameter_map)
示例11: test_lifted_gate_single_qubit
# 需要导入模块: from pyquil import gates [as 别名]
# 或者: from pyquil.gates import H [as 别名]
def test_lifted_gate_single_qubit():
test_unitary = lifted_gate(H(0), 1)
true_unitary = mat.H
assert np.allclose(test_unitary, true_unitary)
test_unitary = lifted_gate(H(0), 5)
true_unitary = np.kron(np.eye(2 ** 4), mat.H)
assert np.allclose(test_unitary, true_unitary)
test_unitary = lifted_gate(RX(0.2, 3), 5)
true_unitary = np.kron(np.eye(2 ** 1), np.kron(mat.RX(0.2), np.eye(2 ** 3)))
assert np.allclose(test_unitary, true_unitary)
test_unitary = lifted_gate(RX(0.5, 4), 5)
true_unitary = np.kron(np.eye(2 ** 0), np.kron(mat.RX(0.5), np.eye(2 ** 4)))
assert np.allclose(test_unitary, true_unitary)
示例12: test_qc_calibration_1q
# 需要导入模块: from pyquil import gates [as 别名]
# 或者: from pyquil.gates import H [as 别名]
def test_qc_calibration_1q(forest):
# noise model with 95% symmetrized readout fidelity per qubit
noise_model = asymmetric_ro_model([0], 0.945, 0.955)
qc = get_qc("1q-qvm")
qc.qam.noise_model = noise_model
# bell state program (doesn't matter)
p = Program()
p += RESET()
p += H(0)
p += CNOT(0, 1)
p.wrap_in_numshots_loop(10000)
# Z experiment
sz = ExperimentSetting(in_state=sZ(0), out_operator=sZ(0))
e = Experiment(settings=[sz], program=p)
results = qc.calibrate(e)
# Z expectation value should just be 1 - 2 * readout_error
np.isclose(results[0].expectation, 0.9, atol=0.01)
assert results[0].total_counts == 20000
示例13: test_qc_calibration_2q
# 需要导入模块: from pyquil import gates [as 别名]
# 或者: from pyquil.gates import H [as 别名]
def test_qc_calibration_2q(forest):
# noise model with 95% symmetrized readout fidelity per qubit
noise_model = asymmetric_ro_model([0, 1], 0.945, 0.955)
qc = get_qc("2q-qvm")
qc.qam.noise_model = noise_model
# bell state program (doesn't matter)
p = Program()
p += RESET()
p += H(0)
p += CNOT(0, 1)
p.wrap_in_numshots_loop(10000)
# ZZ experiment
sz = ExperimentSetting(in_state=sZ(0) * sZ(1), out_operator=sZ(0) * sZ(1))
e = Experiment(settings=[sz], program=p)
results = qc.calibrate(e)
# ZZ expectation should just be (1 - 2 * readout_error_q0) * (1 - 2 * readout_error_q1)
np.isclose(results[0].expectation, 0.81, atol=0.01)
assert results[0].total_counts == 40000
示例14: test_sync_run_and_measure_mock
# 需要导入模块: from pyquil import gates [as 别名]
# 或者: from pyquil.gates import H [as 别名]
def test_sync_run_and_measure_mock(qvm: QVMConnection):
mock_qvm = qvm
mock_endpoint = mock_qvm.sync_endpoint
def mock_response(request, context):
assert json.loads(request.text) == {
"type": "multishot-measure",
"qubits": [0, 1],
"trials": 2,
"compiled-quil": "H 0\nCNOT 0 1\n",
"rng-seed": 52,
}
return "[[0,0],[1,1]]"
with requests_mock.Mocker() as m:
m.post(mock_endpoint + "/qvm", text=mock_response)
assert mock_qvm.run_and_measure(BELL_STATE, [0, 1], trials=2) == [[0, 0], [1, 1]]
with pytest.raises(ValueError):
mock_qvm.run_and_measure(EMPTY_PROGRAM, [0])
示例15: test_run
# 需要导入模块: from pyquil import gates [as 别名]
# 或者: from pyquil.gates import H [as 别名]
def test_run(forest):
device = NxDevice(nx.complete_graph(3))
qc = QuantumComputer(
name="testy!",
qam=QVM(connection=forest, gate_noise=[0.01] * 3),
device=device,
compiler=DummyCompiler(),
)
bitstrings = qc.run(
Program(
Declare("ro", "BIT", 3),
H(0),
CNOT(0, 1),
CNOT(1, 2),
MEASURE(0, MemoryReference("ro", 0)),
MEASURE(1, MemoryReference("ro", 1)),
MEASURE(2, MemoryReference("ro", 2)),
).wrap_in_numshots_loop(1000)
)
assert bitstrings.shape == (1000, 3)
parity = np.sum(bitstrings, axis=1) % 3
assert 0 < np.mean(parity) < 0.15