Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def ucc_circuit(theta):
"""
Implements
exp(-i theta X_{0}Y_{1})
:param theta: rotation parameter
:return: pyquil.Program
"""
generator = sX(0) * sY(1)
initial_prog = Program().inst(X(1), X(0))
# compiled program
program = initial_prog + exponentiate(
float(theta) * generator
) # float is required because pyquil has weird casting behavior
return program
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
significant qubit to least significant qubit.
The diffusion operator is the diagonal operator given by(1, -1, -1, ..., -1).
:param qubits: A list of ints corresponding to the qubits to operate on. The operator
operates on bistrings of the form |qubits[0], ..., qubits[-1]>.
"""
p = pq.Program()
if len(qubits) == 1:
p.inst(H(qubits[0]))
p.inst(Z(qubits[0]))
p.inst(H(qubits[0]))
else:
p.inst(map(X, qubits))
p.inst(H(qubits[-1]))
p.inst(RZ(-np.pi)(qubits[0]))
p += n_qubit_control(qubits[:-1], qubits[-1], np.array([[0, 1], [1, 0]]), "NOT")
p.inst(RZ(-np.pi)(qubits[0]))
p.inst(H(qubits[-1]))
p.inst(map(X, qubits))
return p
are those reserved to represent the pointer. The first N - P qubits
are the qubits which the one-qubit gate U can act on.
"""
ptr_bits = int(floor(np.log2(num_qubits)))
data_bits = num_qubits - ptr_bits
ptr_state = 0
assert ptr_bits > 0
program = pq.Program()
program.defgate("CU", controlled(ptr_bits, U))
for _, target_qubit, changed in gray(ptr_bits):
if changed is None:
for ptr_qubit in range(num_qubits - ptr_bits, num_qubits):
program.inst(X(ptr_qubit))
ptr_state ^= 1 << (ptr_qubit - data_bits)
else:
program.inst(X(data_bits + changed))
ptr_state ^= 1 << changed
if target_qubit < data_bits:
control_qubits = tuple(data_bits + i for i in range(ptr_bits))
program.inst(("CU",) + control_qubits + (target_qubit,))
fixup(program, data_bits, ptr_bits, ptr_state)
return program
seen_names = set()
for gate in def_gates:
if gate.name not in seen_names:
seen_names.add(gate.name)
unique_gates.append(gate)
many_hadamard = pq.Program().inst(map(H, qubits))
grover_iter = oracle + many_hadamard + diff_op + many_hadamard
# To prevent redefining gates, this is not the preferred way.
grover_iter.defined_gates = []
prog = pq.Program()
prog.defined_gates = unique_gates
# Initialize ancilla to be in the minus state
prog.inst(X(query_qubit))
prog.inst(H(query_qubit))
prog += many_hadamard
for _ in xrange(num_iter):
prog += grover_iter
# Move the ancilla back to the zero state
prog.inst(H(query_qubit))
prog.inst(X(query_qubit))
return prog
:param bitstring: The desired bitstring, given as a string of ones and zeros. e.g. "101"
:param qubits: The qubits the oracle is called on, the last of which is the query qubit. The
qubits are assumed to be ordered from most significant qubit to least signicant qubit.
:param query_qubit: The qubit |q> that the oracle write its answer to.
:return: A program representing this oracle.
"""
if len(qubits) != len(bitstring):
raise ValueError("The bitstring should be the same length as the number of qubits.")
if not isinstance(query_qubit, Qubit):
raise ValueError("query_qubit should be a single qubit.")
if not (isinstance(bitstring, str) and all([num in ('0', '1') for num in bitstring])):
raise ValueError("The bitstring must be a string of ones and zeros.")
prog = pq.Program()
for i, qubit in enumerate(qubits):
if bitstring[i] == '0':
prog.inst(X(qubit))
prog += n_qubit_control(qubits, query_qubit, np.array([[0, 1], [1, 0]]), 'NOT')
for i, qubit in enumerate(qubits):
if bitstring[i] == '0':
prog.inst(X(qubit))
return prog
def exp_wrap(param: float) -> Program:
prog = Program()
if is_identity(term):
prog.inst(X(0))
prog.inst(PHASE(-param * coeff, 0))
prog.inst(X(0))
prog.inst(PHASE(-param * coeff, 0))
elif is_zero(term):
pass
else:
prog += _exponentiate_general_case(term, param)
return prog
"""Simple program in pyQuil."""
# Import the pyQuil library
import pyquil
# Create a quantum program
prog = pyquil.Program()
# Declare a classical register
creg = prog.declare("ro", memory_type="BIT", memory_size=1)
# Add a NOT operation and measurement on a qubit
prog += [
pyquil.gates.X(0),
pyquil.gates.MEASURE(0, creg[0])
]
# Print the program
print("Program:")
print(prog)
# Get a quantum computer to run on
computer = pyquil.get_qc("1q-qvm")
# Simulate the program many times
prog.wrap_in_numshots_loop(10)
# Execute the program on the computer
result = computer.run(prog)
def teleport(start_index, end_index, ancilla_index):
"""Teleport a qubit from start to end using an ancilla qubit
"""
p = make_bell_pair(end_index, ancilla_index)
# do the teleportation
p.inst(CNOT(start_index, ancilla_index))
p.inst(H(start_index))
# measure the results and store them in classical registers [0] and [1]
p.measure(start_index, 0)
p.measure(ancilla_index, 1)
p.if_then(1, X(2))
p.if_then(0, Z(2))
p.measure(end_index, 2)
return p