def test_ignore_non_gates(): class Foo(Operator): @property def name(self) -> str: return "foo" def to_ir(self, target): return "foo" circ = Circuit().h(0).h(1).cnot(1, 2).add_instruction(Instruction(Foo(), 0)) expected = ( "T : |0|1|", " ", "q0 : -H---", " ", "q1 : -H-C-", " | ", "q2 : ---X-", "", "T : |0|1|", ) expected = "\n".join(expected) assert AsciiCircuitDiagram.build_diagram(circ) == expected
def openqasm_result_types_bell_pair_testing(device: Device, run_kwargs: Dict[str, Any]): openqasm_string = ("OPENQASM 3;" "qubit[2] q;" "h q[0];" "cnot q[0], q[1];" "#pragma braket result expectation h(q[0]) @ x(q[1])" "#pragma braket result sample h(q[0]) @ x(q[1])") hardcoded_openqasm = OpenQasmProgram(source=openqasm_string) circuit = (Circuit().h(0).cnot(0, 1).expectation( Observable.H() @ Observable.X(), (0, 1)).sample(Observable.H() @ Observable.X(), (0, 1))) generated_openqasm = circuit.to_ir(ir_type=IRType.OPENQASM) for program in hardcoded_openqasm, generated_openqasm: result = device.run(program, **run_kwargs).result() assert len(result.result_types) == 2 assert (0.6 < result.get_value_by_result_type( ResultType.Expectation(observable=Observable.H() @ Observable.X(), target=[0, 1])) < 0.8) assert (len( result.get_value_by_result_type( ResultType.Sample(observable=Observable.H() @ Observable.X(), target=[0, 1]))) == run_kwargs["shots"])
def test_basis_rotation_instructions_multiple_result_types_tensor_product_hermitian( ): circ = (Circuit().h(0).cnot(0, 1).cnot(1, 2).sample( observable=Observable.Hermitian(matrix=np.array([[1, 0], [0, -1]])) @ Observable.H(), target=[0, 1], ).variance( observable=Observable.Hermitian(matrix=np.array([[1, 0], [0, -1]])) @ Observable.H(), target=[0, 1], ).expectation( observable=Observable.Hermitian(matrix=np.array([[0, 1], [1, 0]])), target=[2])) expected = [ Instruction(Gate.Unitary(matrix=np.array([[0, 1], [1, 0]])), target=[0]), Instruction(Gate.Ry(-np.pi / 4), 1), Instruction( Gate.Unitary(matrix=1.0 / np.sqrt(2.0) * np.array([[1.0, 1.0], [1.0, -1.0]], dtype=complex)), target=[2], ), ] assert circ.basis_rotation_instructions == expected
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: gate = translate_operation(operation) 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_multiple_result_types_with_state_vector_amplitude(): circ = (Circuit().cnot(0, 2).cnot(1, 3).h(0).variance( observable=Observable.Y(), target=0).expectation(observable=Observable.Y(), target=3).expectation( observable=Observable.Hermitian(np.array([[1.0, 0.0], [0.0, 1.0]])), target=1).amplitude(["0001"]).state_vector()) expected = ( "T : | 0 |1| Result Types |", " ", "q0 : -C---H-Variance(Y)------------", " | ", "q1 : -|-C---Expectation(Hermitian)-", " | | ", "q2 : -X-|--------------------------", " | ", "q3 : ---X---Expectation(Y)---------", "", "T : | 0 |1| Result Types |", "", "Additional result types: Amplitude(0001), StateVector", ) expected = "\n".join(expected) assert AsciiCircuitDiagram.build_diagram(circ) == expected
def result_types_zero_shots_bell_pair_testing(device: Device, run_kwargs: Dict[str, Any]): circuit = (Circuit().h(0).cnot(0, 1).expectation( observable=Observable.H() @ Observable.X(), target=[0, 1]).state_vector().amplitude(["01", "10", "00", "11"])) result = device.run(circuit, **run_kwargs).result() assert len(result.result_types) == 3 assert np.allclose( result.get_value_by_result_type( ResultType.Expectation(observable=Observable.H() @ Observable.X(), target=[0, 1])), 1 / np.sqrt(2), ) assert np.allclose( result.get_value_by_result_type(ResultType.StateVector()), np.array([1, 0, 0, 1]) / np.sqrt(2), ) assert result.get_value_by_result_type( ResultType.Amplitude(["01", "10", "00", "11"])) == { "01": 0j, "10": 0j, "00": (1 / np.sqrt(2)), "11": (1 / np.sqrt(2)), }
def circuit(): return Circuit().h(0).cnot(0, 1)
def circuit(): return Circuit().h(0)
def test_add_result_type_default(prob): circ = Circuit().add_result_type(prob) assert list(circ.result_types) == [prob]
def cnot_prob(cnot_instr, prob): return Circuit().add_result_type(prob).add_instruction(cnot_instr)
def test_basis_rotation_instructions_target(): circ = Circuit().h(0).cnot(0, 1).expectation(observable=Observable.X(), target=0) expected = [Instruction(Gate.H(), 0)] assert circ.basis_rotation_instructions == expected
def test_ir_empty_instructions_result_types(): circ = Circuit() assert circ.to_ir() == jaqcd.Program(instructions=[], results=[], basis_rotation_instructions=[])
def test_add_with_instruction_with_mapping(cnot_instr): target_mapping = {0: 10, 1: 11} circ = Circuit().add(cnot_instr, target_mapping=target_mapping) expected = Circuit().add_instruction(cnot_instr, target_mapping=target_mapping) assert circ == expected
def test_add_with_instruction_with_default(cnot_instr): circ = Circuit().add(cnot_instr) assert circ == Circuit().add_instruction(cnot_instr)
def test_add_circuit_with_target_and_mapping(h): Circuit().add_circuit(h, target=[10], target_mapping={0: 10})
def test_add_operator(h, bell_pair): addition = h + bell_pair + h + h expected = Circuit().add(h).add(bell_pair).add(h).add(h) assert addition == expected assert addition != (h + h + bell_pair + h)
def foo(target): return Circuit().add(Instruction(Gate.H(), 0))
def test_add_with_instruction_with_target(cnot_instr): target = [10, 11] circ = Circuit().add(cnot_instr, target=target) expected = Circuit().add_instruction(cnot_instr, target=target) assert circ == expected
def h(): return Circuit().add_instruction(Instruction(Gate.H(), 0))
def test_add_with_circuit_with_default(bell_pair): circ = Circuit().add(bell_pair) assert circ == Circuit().add_circuit(bell_pair)
def test_basis_rotation_instructions_multiple_result_types_different_targets(): circ = (Circuit().h(0).cnot(0, 1).expectation( observable=Observable.X(), target=0).sample(observable=Observable.H(), target=1)) expected = [Instruction(Gate.H(), 0), Instruction(Gate.Ry(-np.pi / 4), 1)] assert circ.basis_rotation_instructions == expected
def test_add_with_circuit_with_mapping(bell_pair): target_mapping = {0: 10, 1: 11} circ = Circuit().add(bell_pair, target_mapping=target_mapping) expected = Circuit().add_circuit(bell_pair, target_mapping=target_mapping) assert circ == expected
def bell_pair(prob): return (Circuit().add_instruction(Instruction( Gate.H(), 0)).add_instruction(Instruction(Gate.CNot(), [0, 1])).add_result_type(prob))
def test_add_with_circuit_with_target(bell_pair): target = [10, 11] circ = Circuit().add(bell_pair, target=target) expected = Circuit().add_circuit(bell_pair, target=target) assert circ == expected
# When running in real QPU you must enter the S3 bucket you created during onboarding to Braket in the code as follows my_bucket = f"amazon-braket-your-bucket" # the name of the bucket my_folder = "YourFolder" # the name of the folder in the bucket s3_folder = (my_bucket, my_folder) # set up device device = AwsDevice("arn:aws:braket:::device/qpu/ionq/ionQdevice") # device = AwsDevice("arn:aws:braket:::device/qpu/rigetti/Aspen-8") # Instantiate the local simulator #from braket.devices import LocalSimulator #device = LocalSimulator() execution_windows = device.properties.service.executionWindows print(f'{device.name} availability windows are:\n{execution_windows}\n') # Create the Teleportation Circuit circ = Circuit() # Put the qubit to teleport in a superposition state circ.h(0) # Create the entangled state (qubit 1 reamins in Alice while qubit 2 is sent to Bob) circ.h(1).cnot(1, 2) # Teleportation algorithm circ.cnot(0, 1).h(0) # Do the trick with deferred measurement circ.h(2).cnot(0, 2).h( 2) # Control Z 0 -> 2 (developed because IonQ is not having native Ctrl-Z) circ.cnot(1, 2) # Control X 1 -> 2
def test_circuit_copy(h, bell_pair, cnot_instr): original = Circuit().add(h).add(bell_pair).add(cnot_instr) copy = original.copy() assert copy is not original assert copy == original
def create_circuit(bits: int = 8) -> Circuit: circ = Circuit() for i in range(bits): circ.h(i) return circ
def cnot(): return Circuit().add_instruction(Instruction(Gate.CNot(), [0, 1]))
def test_unitary_matrix_target_size_mismatch(): Circuit().unitary( matrix=np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]), targets=[0] )
def test_circuit_copy_with_modification(h, bell_pair, cnot_instr): original = Circuit().add(h).add(bell_pair) copy = original.copy().add(cnot_instr) assert copy != original