Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Implementation of the quantum portion of Simon's Algorithm.
Given a list of input qubits, all initially in the :math:`\\vert 0\\rangle` state, create a
program that applies the Walsh-Hadamard transform the qubits before and after going through
the oracle.
:return: A program corresponding to the desired instance of Simon's Algorithm.
"""
simon_circuit = Program()
oracle_name = "SIMON_ORACLE"
simon_circuit.defgate(oracle_name, self.unitary_function_mapping)
simon_circuit.inst([H(i) for i in self.computational_qubits])
simon_circuit.inst(tuple([oracle_name] + sorted(self._qubits, reverse=True)))
simon_circuit.inst([H(i) for i in self.computational_qubits])
return simon_circuit
def diffusion_operator(qubits):
"""Constructs the (Grover) diffusion operator on qubits, assuming they are ordered from most
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
def teleport(start_index, end_index, ancilla_index):
"""Teleport a qubit from start to end using an ancilla qubit
"""
program = make_bell_pair(end_index, ancilla_index)
ro = program.declare("ro", memory_size=3)
# do the teleportation
program.inst(CNOT(start_index, ancilla_index))
program.inst(H(start_index))
# measure the results and store them in classical registers [0] and [1]
program.measure(start_index, ro[0])
program.measure(ancilla_index, ro[1])
program.if_then(ro[1], X(2))
program.if_then(ro[0], Z(2))
program.measure(end_index, ro[2])
print(program)
return program
``|qubits[0], ..., qubits[-1]>``.
"""
program = Program()
if len(qubits) == 1:
program.inst(Z(qubits[0]))
else:
program.inst([X(q) for q in qubits])
program.inst(H(qubits[-1]))
program.inst(RZ(-np.pi, qubits[0]))
program += (ControlledProgramBuilder()
.with_controls(qubits[:-1])
.with_target(qubits[-1])
.with_operation(X_GATE)
.with_gate_name(X_GATE_LABEL).build())
program.inst(RZ(-np.pi, qubits[0]))
program.inst(H(qubits[-1]))
program.inst([X(q) for q in qubits])
return program
The full description is available in ../docs/source/exercises.rst
:return: pyQuil Program
"""
prog = pq.Program()
ro = prog.declare("ro", memory_size=2)
picard_register = ro[1]
answer_register = ro[0]
then_branch = pq.Program(X(0))
else_branch = pq.Program(I(0))
# Prepare Qubits in Heads state or superposition, respectively
prog.inst(X(0), H(1))
# Q puts the coin into a superposition
prog.inst(H(0))
# Picard makes a decision and acts accordingly
prog.measure(1, picard_register)
prog.if_then(picard_register, then_branch, else_branch)
# Q undoes his superposition operation
prog.inst(H(0))
# The outcome is recorded into the answer register
prog.measure(0, answer_register)
return prog
except ImportError: # pragma no coverage
cvxpy = None
if not _CVXPY_ERROR_LOGGED:
_log.error("Could not import cvxpy. Tomography tools will not function.")
_CVXPY_ERROR_LOGGED = True
return cvxpy
THREE_COLOR_MAP = ['#48737F', '#FFFFFF', '#D6619E']
rigetti_3_color_cm = LinearSegmentedColormap.from_list("Rigetti", THREE_COLOR_MAP[::-1], N=100)
FIVE_COLOR_MAP = ['#C671A2', '#545253', '#85B5BE', '#ECE9CC', '#C671A2']
rigetti_4_color_cm = LinearSegmentedColormap.from_list("Rigetti", FIVE_COLOR_MAP[::-1], N=100)
EPS = 1e-2
SEED = 137
BELL_STATE_PROGRAM = Program([H(0), H(1), CZ(0, 1), H(1)])
CNOT_PROGRAM = Program([H(1), CZ(0, 1), H(1)])
BAD_1Q_READOUT = np.array([[.9, .15],
[.1, .85]])
BAD_2Q_READOUT = np.kron(BAD_1Q_READOUT, BAD_1Q_READOUT)
# constants to provide optional fancy progress bars
NOTEBOOK_MODE = False
TRANGE = tqdm.trange
def notebook_mode(m):
"""
Configure whether this module should assume that it is being run from a jupyter notebook.
This sets some global variables related to how progress for long measurement sequences is
def k_4_ctqw(t):
"""Returns a program implementing a continuous time quantum walk."""
prog = pq.Program()
# Change to diagonal basis
prog.inst(H(0))
prog.inst(H(1))
# Time evolve
prog.inst(CPHASE00(-4*t, 0, 1))
# Change back to computational basis
prog.inst(H(0))
prog.inst(H(1))
return prog
def die_program(number_of_sides):
"""
Generate a quantum program to roll a die of n faces.
"""
prog = Program()
n_qubits = qubits_needed(number_of_sides)
ro = prog.declare("ro", "BIT", n_qubits)
# Hadamard initialize.
for q in range(n_qubits):
prog.inst(H(q))
# Measure everything.
for q in range(n_qubits):
prog.measure(q, ro[q])
return prog