Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_cirq_to_circuit2():
q0 = cq.GridQubit(0, 0)
q1 = cq.GridQubit(1, 0)
def basic_circuit(meas=False):
sqrt_x = cq.X**0.5
yield cq.X(q0) ** 0.5, sqrt_x(q1)
yield cq.CZ(q0, q1)
yield sqrt_x(q0), sqrt_x(q1)
if meas:
yield cq.measure(q0, key='q0'), cq.measure(q1, key='q1')
cqc = cq.Circuit()
cqc.append(basic_circuit())
print()
print(cqc)
for x in grid_stream.readlines()
if len(x) and x[0] != '#']
# Get the number of rows
grid_I = len(grid)
# Check that the number of columns is consistent
if len(set(len(x) for x in grid)) != 1:
raise AssertionError("Number of columns in grid are not consistent.")
# Get the number of columns
grid_J = len(grid[0])
# Return cirq.GridQubit
return [
cirq.GridQubit(I, J)
for I in range(grid_I)
for J in range(grid_J)
if grid[I][J] == '1'
]
def test_outer_init_of_qubits_error(self):
"""Tests that giving the wrong number of qubits as parameters to CirqDevice raises an error."""
qubits = [
cirq.GridQubit(0, 0),
cirq.GridQubit(0, 1),
cirq.GridQubit(1, 0),
cirq.GridQubit(1, 1),
]
with pytest.raises(
qml.DeviceError,
match="The number of given qubits and the specified number of wires have to match",
):
dev = CirqDevice(3, 100, False, qubits=qubits)
def set_io_qubits(qubit_count):
"""Add the specified number of input and output qubits."""
input_qubits = [cirq.GridQubit(i, 0) for i in range(qubit_count)]
output_qubit = cirq.GridQubit(qubit_count, 0)
return (input_qubits, output_qubit)
def make_bell_test_circuit():
alice = cirq.GridQubit(0, 0)
bob = cirq.GridQubit(1, 0)
alice_referee = cirq.GridQubit(0, 1)
bob_referee = cirq.GridQubit(1, 1)
circuit = cirq.Circuit()
# Prepare shared entangled state.
circuit.append([
cirq.H(alice),
cirq.CNOT(alice, bob),
cirq.X(alice)**-0.25,
])
# Referees flip coins.
circuit.append([
cirq.H(alice_referee),
def main():
# Pick a qubit.
qubit = cirq.GridQubit(0, 0)
# Create a circuit
circuit = cirq.Circuit.from_ops(
cirq.X(qubit)**0.5, # Square root of NOT.
cirq.measure(qubit, key='m') # Measurement.
)
print("Circuit:")
print(circuit)
# Simulate the circuit several times.
simulator = cirq.Simulator()
result = simulator.run(circuit, repetitions=20)
print("Results:")
print(result)
def generate_2x2_grid_qft_circuit():
"""Returns a QFT circuit on a 2 x 2 planar qubit architecture.
Circuit adopted from https://arxiv.org/pdf/quant-ph/0402196.pdf.
"""
# Define a 2*2 square grid of qubits
a, b, c, d = [cirq.GridQubit(0, 0), cirq.GridQubit(0, 1),
cirq.GridQubit(1, 1), cirq.GridQubit(1, 0)]
# Create the Circuit
circuit = cirq.Circuit(
cirq.H(a),
cz_and_swap(a, b, 0.5),
cz_and_swap(b, c, 0.25),
cz_and_swap(c, d, 0.125),
cirq.H(a),
cz_and_swap(a, b, 0.5),
cz_and_swap(b, c, 0.25),
cirq.H(a),
cz_and_swap(a, b, 0.5),
cirq.H(a),
strategy=cirq.InsertStrategy.EARLIEST
)
A : numpy.ndarray
Hermitian matrix that defines the system of equations Ax = b.
C : float
Hyperparameter for HHL.
t : float
Hyperparameter for HHL
C and t are tunable parameters for the algorithm.
register_size is the size of the eigenvalue register.
input_prep_gates is a list of gates to be applied to |0> to generate the
desired input state |b>.
"""
# Ancilla register
ancilla = cirq.GridQubit(0, 0)
# Work register
register = [cirq.GridQubit(i + 1, 0) for i in range(register_size)]
# Input/output register
memory = cirq.GridQubit(register_size + 1, 0)
# Create a circuit
circ = cirq.Circuit()
# Unitary e^{iAt} for QPE
unitary = HamiltonianSimulation(A, t)
# QPE with the unitary e^{iAt}
qpe = QPE(register_size + 1, unitary)
t : float
Hyperparameter for HHL
C and t are tunable parameters for the algorithm.
register_size is the size of the eigenvalue register.
input_prep_gates is a list of gates to be applied to |0> to generate the
desired input state |b>.
"""
# Ancilla register
ancilla = cirq.GridQubit(0, 0)
# Work register
register = [cirq.GridQubit(i + 1, 0) for i in range(register_size)]
# Input/output register
memory = cirq.GridQubit(register_size + 1, 0)
# Create a circuit
circ = cirq.Circuit()
# Unitary e^{iAt} for QPE
unitary = HamiltonianSimulation(A, t)
# QPE with the unitary e^{iAt}
qpe = QPE(register_size + 1, unitary)
# Add state preparation circuit for |b>
circ.append([gate(memory) for gate in input_prep_gates])
# Add the HHL algorithm to the circuit
circ.append([
qpe(*(register + [memory])),
def validate_operation(self, operation):
if not isinstance(operation, cirq.GateOperation):
raise ValueError(
'{!r} is not a supported operation'.format(operation))
if not self.is_qflex_virt_dev_op(operation):
raise ValueError('{!r} is not a supported gate'.format(
operation.gate))
# Are the qubits GridQubits?
# If no -> Error
# TODO: Translate to GridQubits automatically?
for qub in operation.qubits:
if not isinstance(qub, cirq.GridQubit):
raise ValueError('{} is not a grid qubit for gate {!r}'.format(
qub, operation.gate))
# here we check connectivity?
if len(operation.qubits) == 2:
p, q = operation.qubits
if not p.is_adjacent(q):
# we could introduce automatic swap network
raise ValueError('Non-local interaction: {}'.format(
repr(operation)))