Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ham += PauliTerm("Y", q1)*PauliTerm("Z",q2,-0.3)
ham += PauliTerm("Y", q1)*PauliTerm("X",q3, 2.0)
params = [3.0,0.4,4.5]
prepare_ansatz = Program()
param_register = prepare_ansatz.declare(
"params", memory_type="REAL", memory_size=3)
prepare_ansatz.inst(RX(param_register[0], q1))
prepare_ansatz.inst(RY(param_register[1], q2))
prepare_ansatz.inst(RY(param_register[2], q3))
def make_memory_map(params):
return {"params": params}
qubit_mapping = get_default_qubit_mapping(prepare_ansatz)
qvm = get_qc("3q-qvm")
with local_qvm():
cost_fn = PrepareAndMeasureOnQVM(prepare_ansatz, make_memory_map,
qvm=qvm,
hamiltonian=ham,
scalar_cost_function=False,
base_numshots=100,
qubit_mapping=qubit_mapping)
out = cost_fn(params, nshots=10)
assert np.allclose(out, (0.346, 0.07), rtol=1.1)
def main(filepath, qubit_num):
with open(filepath) as file:
quil_prog = file.read()
program = Program(quil_prog)
qc = get_qc(f"{qubit_num}q-qvm")
print("Running Quil Program from: ", filepath)
print("---------------------------")
print("Output: ")
print(qc.run(program))
def run_bell_medium_level(n_shots=1000):
# Step 1. Get a device. Either a QVM or a QPU
qc = get_qc("9q-generic-qvm")
q = [4, 5] # qubits
# Step 2. Construct your program
program = Program()
program += H(q[0])
program += CNOT(q[0], q[1])
# Step 2.1. Manage read-out memory
ro = program.declare("ro", memory_type="BIT", memory_size=2)
program += MEASURE(q[0], ro[0])
program += MEASURE(q[1], ro[1])
# Step 2.2. Run the program in a loop
program = program.wrap_in_numshots_loop(n_shots)
# Step 3. Compile and run
size = 4
num = 3200000
def encode(qid, msg):
assert 0 <= msg < 16
return RY(asin(sqrt(msg/16))*2, qid)
if __name__ == '__main__':
assert len(flag) == size * 2
print(f"Your flag is flag{{{flag}}}")
msg = [int(i, 16) for i in flag]
init = Program()
for i, m in enumerate(msg):
init += encode(i, m)
qc = get_qc('16q-qvm')
with open('program1', 'rb') as f:
main = pickle.load(f)
program = init + main
reg = program.declare('ro', 'BIT', size)
for i in range(size):
program += MEASURE(i, reg[i])
program1 = program
program.wrap_in_numshots_loop(num)
result1 = qc.run(qc.compile(program))
with open('program2', 'rb') as f:
main = pickle.load(f)
program = init + main
reg = program.declare('ro', 'BIT', size)
for i in range(size):
def run_bell_low_level(n_shots=1000):
# Step 1. Get some device components
qc = get_qc("9q-generic-qvm")
compiler = qc.compiler
qam = qc.qam
del qc
q = [4, 5] # qubits
# Step 2. Construct your program
program = Program()
program += H(q[0])
program += CNOT(q[0], q[1])
# Step 2.1. Manage read-out memory
ro = program.declare("ro", memory_type="BIT", memory_size=2)
program += MEASURE(q[0], ro[0])
program += MEASURE(q[1], ro[1])
self.num_qubits = self.pset.num_variables()
qubits = list(range(self.num_qubits))
angles = np.linspace(0, 2 * np.pi, NUM_ANGLES, endpoint=False)
self.instrs = [CNOT(q0, q1) for q0, q1 in product(qubits, qubits) if q0 != q1]
self.instrs += [op(theta, q) for q, op, theta in product(qubits, [RX, RY, RZ], angles)]
self.action_space = gym.spaces.Discrete(len(self.instrs))
obs_len = NUM_SHOTS * self.num_qubits + len(self.pset.problem(0))
self.observation_space = gym.spaces.Box(np.full(obs_len, -1.0), np.full(obs_len, 1.0), dtype=np.float32)
self.reward_threshold = .8
self.qpu = qpu
if qpu:
self._qc = get_qc(QPU_NAME)
else:
self._qc = QuantumComputer(name='qvm',
qam=PyQVM(n_qubits=self.num_qubits),
device=NxDevice(nx.complete_graph(self.num_qubits)),
compiler=MinimalPyQVMCompiler())
self.reset()
name = "CONTROLLED-U{0}".format(2 ** i)
# define the gate
p.defgate(name, cU)
# apply it
p.inst((name, i) + tuple(U_qubits))
# Compute the QFT
p = p + inverse_qft(output_qubits)
# Perform the measurements
for i in output_qubits:
p.measure(i, ro[reg_offset + i])
return p
if __name__ == '__main__':
qc = get_qc("9q-square-qvm")
X = np.asarray([[0.0, 1.0], [1.0, 0.0]])
Y = np.asarray([[0.0, -1.0j], [1.0j, 0.0]])
Rx = expm(1j * X * np.pi / 8)
Ry = expm(1j * Y * np.pi / 16)
U = np.kron(Rx, Ry)
p = phase_estimation(U, 3)
print(p.out())
print(WavefunctionSimulator().wavefunction(p))
num_shots = 100
p.wrap_in_numshots_loop(num_shots)
executable = qc.compiler.native_quil_to_executable(p)
bitstrings = qc.run(executable)
def get_qvm(number_of_sides):
"""
Get a QVM to simulate the requested number of sides.
"""
return get_qc(f"{qubits_needed(number_of_sides)}q-qvm")
# 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)
# Print the results
print("Result:")
print(result)
def run_bell_high_level(n_shots=1000):
# Step 1. Get a device. Either a QVM or a QPU
qc = get_qc("9q-generic-qvm")
q = [4, 5] # qubits
# Step 2. Construct your program
program = Program(H(q[0]), CNOT(q[0], q[1]))
# Step 3. Run
results = qc.run_and_measure(program, trials=n_shots)
# Bincount bitstrings
ints = sum(results[q[i]] * 2 ** i for i in range(len(q)))
print("bincounts", np.bincount(ints))
# Check parity
parities = (results[q[0]] + results[q[1]]) % 2
print("avg parity", np.mean(parities))