def many_layers(n_qubits: int, n_layers: int) -> Circuit: """ Function to return circuit with many layers. :param int n_qubits: number of qubits :param int n_layers: number of layers :return: Constructed easy circuit :rtype: Circuit """ qubits = range(n_qubits) circuit = Circuit() # instantiate circuit object for q in range(n_qubits): circuit.h(q) for layer in range(n_layers): if (layer + 1) % 100 != 0: for qubit in range(len(qubits)): angle = np.random.uniform(0, 2 * math.pi) gate = np.random.choice( [Gate.Rx(angle), Gate.Ry(angle), Gate.Rz(angle), Gate.H()], 1, replace=True )[0] circuit.add_instruction(Instruction(gate, qubit)) else: for q in range(0, n_qubits, 2): circuit.cnot(q, q + 1) for q in range(1, n_qubits - 1, 2): circuit.cnot(q, q + 1) return circuit
def apply(self, operations: Sequence[Operation], rotations: Sequence[Operation] = None, **run_kwargs) -> Circuit: """Instantiate Braket Circuit object.""" rotations = rotations or [] circuit = Circuit() # Add operations to Braket Circuit object for operation in operations + rotations: params = [ p.numpy() if isinstance(p, np.tensor) else p for p in operation.parameters ] gate = translate_operation(operation, params) dev_wires = self.map_wires(operation.wires).tolist() ins = Instruction(gate, dev_wires) circuit.add_instruction(ins) unused = set(range( self.num_wires)) - {int(qubit) for qubit in circuit.qubits} # To ensure the results have the right number of qubits for qubit in sorted(unused): circuit.i(qubit) return circuit
def test_from_braket_parameterized_two_qubit_gates(): pgates = [ braket_gates.CPhaseShift, braket_gates.CPhaseShift00, braket_gates.CPhaseShift01, braket_gates.CPhaseShift10, braket_gates.PSwap, braket_gates.XX, braket_gates.YY, braket_gates.ZZ, braket_gates.XY, ] angles = np.random.RandomState(2).random(len(pgates)) instructions = [ Instruction(rot(a), target=[0, 1]) for rot, a in zip(pgates, angles) ] cirq_circuits = list() for instr in instructions: braket_circuit = BKCircuit() braket_circuit.add_instruction(instr) cirq_circuits.append(from_braket(braket_circuit)) for instr, cirq_circuit in zip(instructions, cirq_circuits): assert np.allclose(instr.operator.to_matrix(), cirq_circuit.unitary())
def test_from_braket_parameterized_single_qubit_gates(qubit_index): braket_circuit = BKCircuit() pgates = [ braket_gates.Rx, braket_gates.Ry, braket_gates.Rz, braket_gates.PhaseShift, ] angles = np.random.RandomState(11).random(len(pgates)) instructions = [ Instruction(rot(a), target=qubit_index) for rot, a in zip(pgates, angles) ] for instr in instructions: braket_circuit.add_instruction(instr) cirq_circuit = from_braket(braket_circuit) for i, op in enumerate(cirq_circuit.all_operations()): assert np.allclose(instructions[i].operator.to_matrix(), protocols.unitary(op)) qubit = LineQubit(qubit_index) expected_cirq_circuit = Circuit( ops.rx(angles[0]).on(qubit), ops.ry(angles[1]).on(qubit), ops.rz(angles[2]).on(qubit), ops.Z.on(qubit)**(angles[3] / np.pi), ) assert _equal(cirq_circuit, expected_cirq_circuit, require_qubit_equality=True)
def test_from_braket_non_parameterized_two_qubit_gates(): braket_circuit = BKCircuit() instructions = [ Instruction(braket_gates.CNot(), target=[2, 3]), Instruction(braket_gates.Swap(), target=[3, 4]), Instruction(braket_gates.ISwap(), target=[2, 3]), Instruction(braket_gates.CZ(), target=(3, 4)), Instruction(braket_gates.CY(), target=(2, 3)), ] for instr in instructions: braket_circuit.add_instruction(instr) cirq_circuit = from_braket(braket_circuit) qreg = LineQubit.range(2, 5) expected_cirq_circuit = Circuit( ops.CNOT(*qreg[:2]), ops.SWAP(*qreg[1:]), ops.ISWAP(*qreg[:2]), ops.CZ(*qreg[1:]), ops.ControlledGate(ops.Y).on(*qreg[:2]), ) assert np.allclose( protocols.unitary(cirq_circuit), protocols.unitary(expected_cirq_circuit), )
def test_from_braket_raises_on_unsupported_gates(): for num_qubits in range(1, 5): braket_circuit = BKCircuit() instr = Instruction( braket_gates.Unitary(_rotation_of_pi_over_7(num_qubits)), target=list(range(num_qubits)), ) braket_circuit.add_instruction(instr) with pytest.raises(ValueError): from_braket(braket_circuit)
def test_from_braket_three_qubit_gates(): braket_circuit = BKCircuit() instructions = [ Instruction(braket_gates.CCNot(), target=[1, 2, 3]), Instruction(braket_gates.CSwap(), target=[1, 2, 3]), ] for instr in instructions: braket_circuit.add_instruction(instr) cirq_circuit = from_braket(braket_circuit) qreg = LineQubit.range(1, 4) expected_cirq_circuit = Circuit(ops.TOFFOLI(*qreg), ops.FREDKIN(*qreg)) assert np.allclose( protocols.unitary(cirq_circuit), protocols.unitary(expected_cirq_circuit), )
def test_from_braket_parameterized_two_qubit_gates(): braket_circuit = BKCircuit() # TODO: Add all two-qubit parameterized gates once translated. pgates = [ braket_gates.CPhaseShift, braket_gates.XX, braket_gates.YY, braket_gates.ZZ, braket_gates.XY, ] angles = np.random.RandomState(2).random(len(pgates)) instructions = [ Instruction(rot(a), target=[0, 1]) for rot, a in zip(pgates, angles) ] for instr in instructions: braket_circuit.add_instruction(instr) cirq_circuit = from_braket(braket_circuit) for i, op in enumerate(cirq_circuit.all_operations()): assert np.allclose(instructions[i].operator.to_matrix(), protocols.unitary(op))
def test_from_braket_non_parameterized_single_qubit_gates(): braket_circuit = BKCircuit() instructions = [ Instruction(braket_gates.I(), target=0), Instruction(braket_gates.X(), target=1), Instruction(braket_gates.Y(), target=2), Instruction(braket_gates.Z(), target=3), Instruction(braket_gates.H(), target=0), Instruction(braket_gates.S(), target=1), Instruction(braket_gates.Si(), target=2), Instruction(braket_gates.T(), target=3), Instruction(braket_gates.Ti(), target=0), Instruction(braket_gates.V(), target=1), Instruction(braket_gates.Vi(), target=2), ] for instr in instructions: braket_circuit.add_instruction(instr) cirq_circuit = from_braket(braket_circuit) for i, op in enumerate(cirq_circuit.all_operations()): assert np.allclose( instructions[i].operator.to_matrix(), protocols.unitary(op) ) qreg = LineQubit.range(4) expected_cirq_circuit = Circuit( ops.I(qreg[0]), ops.X(qreg[1]), ops.Y(qreg[2]), ops.Z(qreg[3]), ops.H(qreg[0]), ops.S(qreg[1]), ops.S(qreg[2]) ** -1, ops.T(qreg[3]), ops.T(qreg[0]) ** -1, ops.X(qreg[1]) ** 0.5, ops.X(qreg[2]) ** -0.5, ) assert _equal(cirq_circuit, expected_cirq_circuit)