Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _construct_deutsch_jozsa_circuit(self):
"""
Builds the Deutsch-Jozsa circuit. Which can determine whether a function f mapping
:math:`\{0,1\}^n \to \{0,1\}` is constant or balanced, provided that it is one of them.
:return: A program corresponding to the desired instance of Deutsch Jozsa's Algorithm.
:rtype: Program
"""
dj_prog = Program()
# Put the first ancilla qubit (query qubit) into minus state
dj_prog.inst(X(self.ancillas[0]), H(self.ancillas[0]))
# Apply Hadamard, Oracle, and Hadamard again
dj_prog.inst([H(qubit) for qubit in self.computational_qubits])
# Build the oracle
oracle_prog = Program()
oracle_prog.defgate(ORACLE_GATE_NAME, self.unitary_matrix)
scratch_bit = self.ancillas[1]
qubits_for_funct = [scratch_bit] + self.computational_qubits
oracle_prog.inst(tuple([ORACLE_GATE_NAME] + qubits_for_funct))
dj_prog += oracle_prog
def _construct_grover_circuit(self) -> None:
"""
Constructs an instance of Grover's Algorithm, using initialized values.
:return: None
"""
oracle = Program()
oracle_name = "GROVER_ORACLE"
oracle.defgate(oracle_name, self.unitary_function_mapping)
oracle.inst(tuple([oracle_name] + self.qubits))
self.grover_circuit = self.oracle_grover(oracle, self.qubits)
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
def bit_reversal(qubits: List[int]) -> Program:
"""
Generate a circuit to do bit reversal.
:param qubits: Qubits to do bit reversal with.
:return: A program to do bit reversal.
"""
p = Program()
n = len(qubits)
for i in range(int(n / 2)):
p.inst(SWAP(qubits[i], qubits[-i - 1]))
return p
if label == "X":
if index == 0:
return Program(RY(pi / 2, qubit))
else:
return Program(RY(-pi / 2, qubit))
elif label == "Y":
if index == 0:
return Program(RX(-pi / 2, qubit))
else:
return Program(RX(pi / 2, qubit))
elif label == "Z":
if index == 0:
return Program()
else:
return Program(RX(pi, qubit))
raise ValueError(f"Bad Pauli label: {label}")
def _one_q_pauli_prep(label, index, qubit):
"""Prepare the index-th eigenstate of the pauli operator given by label."""
if index not in [0, 1]:
raise ValueError(f"Bad Pauli index: {index}")
if label == "X":
if index == 0:
return Program(RY(pi / 2, qubit))
else:
return Program(RY(-pi / 2, qubit))
elif label == "Y":
if index == 0:
return Program(RX(-pi / 2, qubit))
else:
return Program(RX(pi / 2, qubit))
elif label == "Z":
if index == 0:
return Program()
else:
return Program(RX(pi, qubit))
raise ValueError(f"Bad Pauli label: {label}")
Parameters
----------
params:
The parameters of the QAOA circuit.
Returns
-------
Program
Parametric Quil Program with the annealing circuit.
"""
(reg, qubits_singles, qubits_pairs, n_steps) =\
(params.reg, params.qubits_singles,
params.qubits_pairs, params.n_steps)
p = Program()
# create list of memory references to store angles in.
# Has to be so nasty, because aliased memories are not supported yet.
# Also length 0 memory references crash the QVM
betas = []
gammas_singles = []
gammas_pairs = []
for i in range(n_steps):
beta = p.declare('x_rotation_angles{}'.format(i),
memory_type='REAL',
memory_size=len(reg))
betas.append(beta)
if not reg: # remove length 0 references again
p.pop()
gamma_singles = p.declare('z_rotation_angles{}'.format(i),
memory_type='REAL',
def diffusion_program(qubits: List[int]) -> Program:
diffusion_program = Program()
dim = 2 ** len(qubits)
hadamard_diffusion_matrix = np.diag([1.0] + [-1.0] * (dim - 1))
diffusion_program.defgate(HADAMARD_DIFFUSION_LABEL, hadamard_diffusion_matrix)
instruction_tuple = (HADAMARD_DIFFUSION_LABEL,) + tuple(qubits)
diffusion_program.inst(instruction_tuple)
return diffusion_program
def _local_pauli_eig_meas(op, idx):
"""
Generate gate sequence to measure in the eigenbasis of a Pauli operator, assuming
we are only able to measure in the Z eigenbasis. (Note: The unitary operations of this
Program are essentially the Hermitian conjugates of those in :py:func:`_one_q_pauli_prep`)
"""
if op == "X":
return Program(RY(-pi / 2, idx))
elif op == "Y":
return Program(RX(pi / 2, idx))
elif op == "Z":
return Program()
raise ValueError(f"Unknown operation {op}")