def result_obj_5(task_metadata_shots): return GateModelTaskResult( taskMetadata=task_metadata_shots, additionalMetadata=AdditionalMetadata(action=jaqcd.Program( instructions=[ jaqcd.CNot(control=0, target=1), jaqcd.CNot(control=2, target=3) ], results=[ jaqcd.Probability(targets=[1]), jaqcd.Expectation(observable=["z"]) ], )), measurements=[ [0, 0, 1, 0], [1, 1, 1, 1], [1, 0, 0, 1], [0, 0, 1, 0], [1, 1, 1, 1], [0, 1, 1, 1], [0, 0, 0, 1], [0, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 1], ], measuredQubits=[0, 1, 2, 3], )
def test_calculate_ir_results(ir_result, expected_result): ir_string = jaqcd.Program( instructions=[jaqcd.H(target=i) for i in range(4)], results=[ir_result] ).json() measured_qubits = [0, 1, 2, 3] measurements = np.array( [ [0, 0, 1, 0], [1, 1, 1, 1], [1, 0, 0, 1], [0, 0, 1, 0], [1, 1, 1, 1], [0, 1, 1, 1], [0, 0, 0, 1], [0, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 1], ] ) result_types = GateModelQuantumTaskResult._calculate_result_types( ir_string, measurements, measured_qubits ) assert len(result_types) == 1 assert result_types[0].type == ir_result assert np.allclose(result_types[0].value, expected_result)
def test_ir_non_empty_instructions_result_types(): circ = Circuit().h(0).cnot(0, 1).probability([0, 1]) expected = jaqcd.Program( instructions=[jaqcd.H(target=0), jaqcd.CNot(control=0, target=1)], results=[jaqcd.Probability(targets=[0, 1])], basis_rotation_instructions=[], ) assert circ.to_ir() == expected
def test_ir_non_empty_instructions_result_types_basis_rotation_instructions(): circ = Circuit().h(0).cnot(0, 1).sample(observable=Observable.X(), target=[0]) expected = jaqcd.Program( instructions=[jaqcd.H(target=0), jaqcd.CNot(control=0, target=1)], results=[jaqcd.Sample(observable=["x"], targets=[0])], basis_rotation_instructions=[jaqcd.H(target=0)], ) assert circ.to_ir() == expected
def _generate_circuit(num_qubits, num_layers, results): instructions = [] for layer in range(num_layers): for qubit in range(num_qubits): instructions.extend([ jaqcd.Rx(target=qubit, angle=0.15), jaqcd.Ry(target=qubit, angle=0.16), jaqcd.Rz(target=qubit, angle=0.17), ]) if qubit > 0: instructions.extend([jaqcd.CZ(control=0, target=qubit)]) return jaqcd.Program(instructions=instructions, results=results)
def _qft_operations(qubit_count): qft_ops = [] for target_qubit in range(qubit_count): angle = np.pi / 2 qft_ops.append(jaqcd.H(target=target_qubit)) for control_qubit in range(target_qubit + 1, qubit_count): qft_ops.append( jaqcd.CPhaseShift(control=control_qubit, target=target_qubit, angle=angle)) angle /= 2 amplitudes = [ "".join([str(random.randint(0, 1)) for _ in range(qubit_count)]) for _ in range(2**(qubit_count // 2)) ] return jaqcd.Program( instructions=qft_ops, results=[ jaqcd.StateVector(), jaqcd.Amplitude(states=amplitudes), jaqcd.Expectation(observable=["x"]), ], )
def test_ir_empty_instructions_result_types(): circ = Circuit() assert circ.to_ir() == jaqcd.Program(instructions=[], results=[], basis_rotation_instructions=[])
def additional_metadata(): program = jaqcd.Program(instructions=[jaqcd.CNot(control=0, target=1)]) return AdditionalMetadata(action=program)