def test_psx_zsx_special_cases(self): """Test decompositions of psx and zsx at special values of parameters""" oqed_psx = OneQubitEulerDecomposer(basis='PSX') oqed_zsx = OneQubitEulerDecomposer(basis='ZSX') theta = np.pi / 3 phi = np.pi / 5 lam = np.pi / 7 test_gates = [ UGate(np.pi, phi, lam), UGate(-np.pi, phi, lam), # test abs(lam + phi + theta) near 0 UGate(np.pi, np.pi / 3, 2 * np.pi / 3), # test theta=pi/2 UGate(np.pi / 2, phi, lam), # test theta=pi/2 and theta+lam=0 UGate(np.pi / 2, phi, -np.pi / 2), # test theta close to 3*pi/2 and theta+phi=2*pi UGate(3 * np.pi / 2, np.pi / 2, lam), # test theta 0 UGate(0, phi, lam), # test phi 0 UGate(theta, 0, lam), # test lam 0 UGate(theta, phi, 0) ] for gate in test_gates: unitary = gate.to_matrix() qc_psx = oqed_psx(unitary) qc_zsx = oqed_zsx(unitary) self.assertTrue(np.allclose(unitary, Operator(qc_psx).data)) self.assertTrue(np.allclose(unitary, Operator(qc_zsx).data))
def test_insert_midmeas_hahn_asap(self): """Test a single X gate as Hahn echo can absorb in the upstream circuit. ┌──────────────────┐ ┌────────────────┐┌─────────┐» q_0: ────────■─────────┤ U(3π/4,-π/2,π/2) ├─┤ Delay(600[dt]) ├┤ Rx(π/4) ├» ┌─┴─┐ └──────────────────┘┌┴────────────────┤└─────────┘» q_1: ──────┤ X ├────────────────■──────────┤ Delay(1000[dt]) ├─────■─────» ┌─────┴───┴──────┐ ┌─┴─┐ └───────┬─┬───────┘ ┌─┴─┐ » q_2: ┤ Delay(700[dt]) ├───────┤ X ├────────────────┤M├───────────┤ X ├───» └────────────────┘ └───┘ └╥┘ └───┘ » c: 1/═══════════════════════════════════════════════╩════════════════════» 0 » « ┌────────────────┐ «q_0: ┤ Delay(600[dt]) ├──■── « └────────────────┘┌─┴─┐ «q_1: ──────────────────┤ X ├ « ┌────────────────┐└───┘ «q_2: ┤ Delay(700[dt]) ├───── « └────────────────┘ «c: 1/═══════════════════════ « """ dd_sequence = [RXGate(pi / 4)] pm = PassManager([ ASAPScheduleAnalysis(self.durations), PadDynamicalDecoupling(self.durations, dd_sequence), ]) midmeas_dd = pm.run(self.midmeas) combined_u = UGate(3 * pi / 4, -pi / 2, pi / 2) expected = QuantumCircuit(3, 1) expected.cx(0, 1) expected.compose(combined_u, [0], inplace=True) expected.delay(600, 0) expected.rx(pi / 4, 0) expected.delay(600, 0) expected.delay(700, 2) expected.cx(1, 2) expected.delay(1000, 1) expected.measure(2, 0) expected.cx(1, 2) expected.cx(0, 1) expected.delay(700, 2) self.assertEqual(midmeas_dd, expected) # check the absorption into U was done correctly self.assertTrue( Operator(XGate()).equiv( Operator(UGate(3 * pi / 4, -pi / 2, pi / 2)) & Operator(RXGate(pi / 4))))
def test_get_instructions_for_qargs(self): with self.assertRaises(KeyError): self.empty_target.operations_for_qargs((0, )) expected = [RZGate(self.theta), IGate(), SXGate(), XGate(), Measure()] res = self.ibm_target.operations_for_qargs((0, )) for gate in expected: self.assertIn(gate, res) expected = [ECRGate()] res = self.fake_backend_target.operations_for_qargs((1, 0)) for gate in expected: self.assertIn(gate, res) expected = [CXGate()] res = self.fake_backend_target.operations_for_qargs((0, 1)) self.assertEqual(expected, res) ideal_sim_expected = [ UGate(self.theta, self.phi, self.lam), RXGate(self.theta), RYGate(self.theta), RZGate(self.theta), CXGate(), ECRGate(), CCXGate(), Measure(), ] for gate in ideal_sim_expected: self.assertIn(gate, self.ideal_sim_target.operations_for_qargs(None))
def test_loading_all_qelib1_gates(self): """Test setting up a circuit with all gates defined in qiskit/qasm/libs/qelib1.inc.""" from qiskit.circuit.library import U1Gate, U2Gate, U3Gate, CU1Gate, CU3Gate, UGate all_gates_qasm = os.path.join(self.qasm_dir, "all_gates.qasm") qasm_circuit = QuantumCircuit.from_qasm_file(all_gates_qasm) ref_circuit = QuantumCircuit(3, 3) # abstract gates (legacy) ref_circuit.append(UGate(0.2, 0.1, 0.6), [0]) ref_circuit.cx(0, 1) # the hardware primitives ref_circuit.append(U3Gate(0.2, 0.1, 0.6), [0]) ref_circuit.append(U2Gate(0.1, 0.6), [0]) ref_circuit.append(U1Gate(0.6), [0]) ref_circuit.id(0) ref_circuit.cx(0, 1) # the standard single qubit gates ref_circuit.u(0.2, 0.1, 0.6, 0) ref_circuit.p(0.6, 0) ref_circuit.x(0) ref_circuit.y(0) ref_circuit.z(0) ref_circuit.h(0) ref_circuit.s(0) ref_circuit.t(0) ref_circuit.sdg(0) ref_circuit.tdg(0) ref_circuit.sx(0) ref_circuit.sxdg(0) # the standard rotations ref_circuit.rx(0.1, 0) ref_circuit.ry(0.1, 0) ref_circuit.rz(0.1, 0) # the barrier ref_circuit.barrier() # the standard user-defined gates ref_circuit.swap(0, 1) ref_circuit.cswap(0, 1, 2) ref_circuit.cy(0, 1) ref_circuit.cz(0, 1) ref_circuit.ch(0, 1) ref_circuit.csx(0, 1) ref_circuit.append(CU1Gate(0.6), [0, 1]) ref_circuit.append(CU3Gate(0.2, 0.1, 0.6), [0, 1]) ref_circuit.cp(0.6, 0, 1) ref_circuit.cu(0.2, 0.1, 0.6, 0, 0, 1) ref_circuit.ccx(0, 1, 2) ref_circuit.crx(0.6, 0, 1) ref_circuit.cry(0.6, 0, 1) ref_circuit.crz(0.6, 0, 1) ref_circuit.rxx(0.2, 0, 1) ref_circuit.rzz(0.2, 0, 1) ref_circuit.measure([0, 1, 2], [0, 1, 2]) self.assertEqual(qasm_circuit, ref_circuit)
def test_operations(self): self.assertEqual(self.empty_target.operations, []) ibm_expected = [ RZGate(self.theta), IGate(), SXGate(), XGate(), CXGate(), Measure() ] for gate in ibm_expected: self.assertIn(gate, self.ibm_target.operations) aqt_expected = [ RZGate(self.theta), RXGate(self.theta), RYGate(self.theta), RGate(self.theta, self.phi), RXXGate(self.theta), ] for gate in aqt_expected: self.assertIn(gate, self.aqt_target.operations) fake_expected = [ UGate(self.fake_backend._theta, self.fake_backend._phi, self.fake_backend._lam), CXGate(), Measure(), ECRGate(), RXGate(math.pi / 6), RXGate(self.fake_backend._theta), ] for gate in fake_expected: self.assertIn(gate, self.fake_backend_target.operations) ideal_sim_expected = [ UGate(self.theta, self.phi, self.lam), RXGate(self.theta), RYGate(self.theta), RZGate(self.theta), CXGate(), ECRGate(), CCXGate(), Measure(), ] for gate in ideal_sim_expected: self.assertIn(gate, self.ideal_sim_target.operations)
def test_optimize_u_basis_u(self): """U(pi/2, pi/3, pi/4) (basis[u3]) -> U(pi/2, pi/3, pi/4)""" qr = QuantumRegister(1, "qr") circuit = QuantumCircuit(qr) circuit.append(UGate(np.pi / 2, np.pi / 3, np.pi / 4), [qr[0]]) passmanager = PassManager() passmanager.append(Optimize1qGates(["u"])) result = passmanager.run(circuit) self.assertEqual(circuit, result)
def test_global_phase_u_on_left(self): """Check proper phase accumulation with instruction with no definition.""" qr = QuantumRegister(1) qc = QuantumCircuit(qr) u1 = U1Gate(0.1) u1.definition.global_phase = np.pi / 2 qc.append(u1, [0]) qc.global_phase = np.pi / 3 qc.append(UGate(0.1, 0.2, 0.3), [0]) dag = circuit_to_dag(qc) after = Optimize1qGates(["u1", "u2", "u", "cx"]).run(dag) self.assertAlmostEqual(after.global_phase, 5 * np.pi / 6, 8)
def test_insert_midmeas_hahn_alap(self): """Test a single X gate as Hahn echo can absorb in the downstream circuit. global phase: 3π/2 ┌────────────────┐ ┌───┐ ┌────────────────┐» q_0: ────────■─────────┤ Delay(625[dt]) ├───────┤ X ├───────┤ Delay(625[dt]) ├» ┌─┴─┐ └────────────────┘┌──────┴───┴──────┐└────────────────┘» q_1: ──────┤ X ├───────────────■─────────┤ Delay(1000[dt]) ├────────■─────────» ┌─────┴───┴──────┐ ┌─┴─┐ └───────┬─┬───────┘ ┌─┴─┐ » q_2: ┤ Delay(700[dt]) ├──────┤ X ├───────────────┤M├──────────────┤ X ├───────» └────────────────┘ └───┘ └╥┘ └───┘ » c: 1/═════════════════════════════════════════════╩═══════════════════════════» 0 » « ┌───────────────┐ «q_0: ┤ U(0,π/2,-π/2) ├───■── « └───────────────┘ ┌─┴─┐ «q_1: ──────────────────┤ X ├ « ┌────────────────┐└───┘ «q_2: ┤ Delay(700[dt]) ├───── « └────────────────┘ «c: 1/═══════════════════════ """ dd_sequence = [XGate()] pm = PassManager([ ALAPScheduleAnalysis(self.durations), PadDynamicalDecoupling(self.durations, dd_sequence), ]) midmeas_dd = pm.run(self.midmeas) combined_u = UGate(0, pi / 2, -pi / 2) expected = QuantumCircuit(3, 1) expected.cx(0, 1) expected.delay(625, 0) expected.x(0) expected.delay(625, 0) expected.compose(combined_u, [0], inplace=True) expected.delay(700, 2) expected.cx(1, 2) expected.delay(1000, 1) expected.measure(2, 0) expected.cx(1, 2) expected.cx(0, 1) expected.delay(700, 2) expected.global_phase = 4.71238898038469 self.assertEqual(midmeas_dd, expected) # check the absorption into U was done correctly self.assertEqual(Operator(combined_u), Operator(XGate()) & Operator(XGate()))
def test_optimize_u_basis_phase_gate(self): """U(0, 0, pi/4) -> p(pi/4). Basis [p].""" qr = QuantumRegister(2, "qr") circuit = QuantumCircuit(qr) circuit.append(UGate(0, 0, np.pi / 4), [qr[0]]) expected = QuantumCircuit(qr) expected.append(PhaseGate(np.pi / 4), [qr[0]]) passmanager = PassManager() passmanager.append(Optimize1qGates(["p"])) result = passmanager.run(circuit) self.assertEqual(expected, result)
def test_optimize_u1_basis_u2_u(self): """U1(pi/4) -> U3(0, 0, pi/4). Basis [u2, u3].""" qr = QuantumRegister(1, "qr") circuit = QuantumCircuit(qr) circuit.append(U1Gate(np.pi / 4), [qr[0]]) expected = QuantumCircuit(qr) expected.append(UGate(0, 0, np.pi / 4), [qr[0]]) passmanager = PassManager() passmanager.append(Optimize1qGates(["u2", "u"])) result = passmanager.run(circuit) self.assertEqual(expected, result)
def test_optimize_u_basis_u1(self): """U(0, 0, pi/4) -> U1(pi/4). Basis [u1].""" qr = QuantumRegister(2, 'qr') circuit = QuantumCircuit(qr) circuit.append(UGate(0, 0, np.pi / 4), [qr[0]]) expected = QuantumCircuit(qr) expected.append(U1Gate(np.pi / 4), [qr[0]]) passmanager = PassManager() passmanager.append(Optimize1qGates(['u1'])) result = passmanager.run(circuit) self.assertEqual(expected, result)
def test_optimize_u3_basis_u(self): """U3(pi/2, pi/3, pi/4) (basis[u3]) -> U(pi/2, pi/3, pi/4)""" qr = QuantumRegister(1, 'qr') circuit = QuantumCircuit(qr) circuit.append(U3Gate(np.pi / 2, np.pi / 3, np.pi / 4), [qr[0]]) passmanager = PassManager() passmanager.append(Optimize1qGates(['u'])) result = passmanager.run(circuit) expected = QuantumCircuit(qr) expected.append(UGate(np.pi / 2, np.pi / 3, np.pi / 4), [qr[0]]) self.assertEqual(expected, result)
def test_all_gates_not_in_ideal_sim_target(self): """Test with target that has ideal gates.""" target = Target() target.add_instruction(HGate()) target.add_instruction(UGate(0, 0, 0)) target.add_instruction(Measure()) property_set = {} analysis_pass = GatesInBasis(target=target) circuit = QuantumCircuit(2) circuit.h(0) circuit.cx(0, 1) circuit.measure_all() analysis_pass(circuit, property_set=property_set) self.assertFalse(property_set["all_gates_in_basis"])
def test_instructions(self): self.assertEqual(self.empty_target.instructions, []) ibm_expected = [ (IGate(), (0, )), (IGate(), (1, )), (IGate(), (2, )), (IGate(), (3, )), (IGate(), (4, )), (RZGate(self.theta), (0, )), (RZGate(self.theta), (1, )), (RZGate(self.theta), (2, )), (RZGate(self.theta), (3, )), (RZGate(self.theta), (4, )), (SXGate(), (0, )), (SXGate(), (1, )), (SXGate(), (2, )), (SXGate(), (3, )), (SXGate(), (4, )), (XGate(), (0, )), (XGate(), (1, )), (XGate(), (2, )), (XGate(), (3, )), (XGate(), (4, )), (CXGate(), (3, 4)), (CXGate(), (4, 3)), (CXGate(), (3, 1)), (CXGate(), (1, 3)), (CXGate(), (1, 2)), (CXGate(), (2, 1)), (CXGate(), (0, 1)), (CXGate(), (1, 0)), (Measure(), (0, )), (Measure(), (1, )), (Measure(), (2, )), (Measure(), (3, )), (Measure(), (4, )), ] self.assertEqual(ibm_expected, self.ibm_target.instructions) ideal_sim_expected = [ (UGate(self.theta, self.phi, self.lam), None), (RXGate(self.theta), None), (RYGate(self.theta), None), (RZGate(self.theta), None), (CXGate(), None), (ECRGate(), None), (CCXGate(), None), (Measure(), None), ] self.assertEqual(ideal_sim_expected, self.ideal_sim_target.instructions)
class TestParameterCtrlState(QiskitTestCase): """Test gate equality with ctrl_state parameter.""" @data((RXGate(0.5), CRXGate(0.5)), (RYGate(0.5), CRYGate(0.5)), (RZGate(0.5), CRZGate(0.5)), (XGate(), CXGate()), (YGate(), CYGate()), (ZGate(), CZGate()), (U1Gate(0.5), CU1Gate(0.5)), (PhaseGate(0.5), CPhaseGate(0.5)), (SwapGate(), CSwapGate()), (HGate(), CHGate()), (U3Gate(0.1, 0.2, 0.3), CU3Gate(0.1, 0.2, 0.3)), (UGate(0.1, 0.2, 0.3), CUGate(0.1, 0.2, 0.3, 0))) @unpack def test_ctrl_state_one(self, gate, controlled_gate): """Test controlled gates with ctrl_state See https://github.com/Qiskit/qiskit-terra/pull/4025 """ self.assertEqual(gate.control(1, ctrl_state='1'), controlled_gate)
def control(num_ctrl_qubits=1, label=None, ctrl_state=None): qr_state = QuantumRegister(self.num_state_qubits + 1) if self.num_state_qubits > 1: qr_ancilla = AncillaRegister(max(1, self.num_state_qubits - 1)) qc_control = QuantumCircuit(qr_state, qr_ancilla, name="off_diags") else: qc_control = QuantumCircuit(qr_state, name="off_diags") qr_ancilla = None # Control will be qr[0] q_control = qr_state[0] qr = qr_state[1:] qc_control.cu(-2 * theta, 3 * np.pi / 2, np.pi / 2, 0, q_control, qr[0]) for i in range(0, self.num_state_qubits - 1): q_controls = [] q_controls.append(q_control) qc_control.cx(qr[i], qr[i + 1]) q_controls.append(qr[i + 1]) # Now we want controlled by 0 qc_control.x(qr[i]) for j in range(i, 0, -1): qc_control.cx(qr[i], qr[j - 1]) q_controls.append(qr[j - 1]) qc_control.x(qr[i]) # Multicontrolled x rotation if len(q_controls) > 1: ugate = UGate(-2 * theta, 3 * np.pi / 2, np.pi / 2) qc_control.append( MCMTVChain(ugate, len(q_controls), 1), q_controls[:] + [qr[i]] + qr_ancilla[:len(q_controls) - 1], ) else: qc_control.cu(-2 * theta, 3 * np.pi / 2, np.pi / 2, 0, q_controls[0], qr[i]) # Uncompute qc_control.x(qr[i]) for j in range(0, i): qc_control.cx(qr[i], qr[j]) qc_control.x(qr[i]) qc_control.cx(qr[i], qr[i + 1]) return qc_control
def test_optimize_u_basis_u2_cx(self): """U(pi/2, 0, pi/4) -> U2(0, pi/4). Basis [u2, cx].""" qr = QuantumRegister(2, "qr") circuit = QuantumCircuit(qr) circuit.append(UGate(np.pi / 2, 0, np.pi / 4), [qr[0]]) circuit.cx(qr[0], qr[1]) expected = QuantumCircuit(qr) expected.append(U2Gate(0, np.pi / 4), [qr[0]]) expected.cx(qr[0], qr[1]) passmanager = PassManager() passmanager.append(Optimize1qGates(["u2", "cx"])) result = passmanager.run(circuit) self.assertEqual(expected, result)
def test_instruction_supported_multiple_parameters(self): target = Target(1) target.add_instruction( UGate(self.theta, self.phi, self.lam), {(0, ): InstructionProperties(duration=270.22e-9, error=0.00713)}, ) self.assertFalse( target.instruction_supported("u", parameters=[math.pi])) self.assertTrue( target.instruction_supported( "u", parameters=[math.pi, math.pi, math.pi])) self.assertTrue( target.instruction_supported( operation_class=UGate, parameters=[math.pi, math.pi, math.pi])) self.assertFalse( target.instruction_supported(operation_class=UGate, parameters=[Parameter("x")]))
def test_instruction_schedule_map_ideal_sim_backend(self): ideal_sim_target = Target(num_qubits=3) theta = Parameter("theta") phi = Parameter("phi") lam = Parameter("lambda") for inst in [ UGate(theta, phi, lam), RXGate(theta), RYGate(theta), RZGate(theta), CXGate(), ECRGate(), CCXGate(), Measure(), ]: ideal_sim_target.add_instruction(inst, {None: None}) inst_map = ideal_sim_target.instruction_schedule_map() self.assertEqual(InstructionScheduleMap(), inst_map)
def setUp(self): super().setUp() self.target = Target() # U gate in qubit 0. self.theta = Parameter("theta") self.phi = Parameter("phi") self.lam = Parameter("lambda") u_props = { (0, ): InstructionProperties(duration=5.23e-8, error=0.00038115), } self.target.add_instruction(UGate(self.theta, self.phi, self.lam), u_props) # Rz gate in qubit 1. rz_props = { (1, ): InstructionProperties(duration=0.0, error=0), } self.target.add_instruction(RZGate(self.phi), rz_props) # X gate in qubit 1. x_props = { (1, ): InstructionProperties(duration=3.5555555555555554e-08, error=0.00020056469709026198), } self.target.add_instruction(XGate(), x_props) # SX gate in qubit 1. sx_props = { (1, ): InstructionProperties(duration=3.5555555555555554e-08, error=0.00020056469709026198), } self.target.add_instruction(SXGate(), sx_props) cx_props = { (0, 1): InstructionProperties(duration=5.23e-7, error=0.00098115), (1, 0): InstructionProperties(duration=4.52e-7, error=0.00132115), } self.target.add_instruction(CXGate(), cx_props)
def test_reverse_direction(self, opt_level): target = Target(2) target.add_instruction(CXGate(), {(0, 1): InstructionProperties(error=1.2e-6)}) target.add_instruction(ECRGate(), {(0, 1): InstructionProperties(error=1.2e-7)}) target.add_instruction( UGate(Parameter("theta"), Parameter("phi"), Parameter("lam")), {(0,): None, (1,): None} ) qr = QuantumRegister(2) circ = QuantumCircuit(qr) circ.append(random_unitary(4, seed=1), [1, 0]) tqc = transpile( circ, target=target, optimization_level=opt_level, translation_method="synthesis", layout_method="trivial", ) tqc_index = {qubit: index for index, qubit in enumerate(tqc.qubits)} self.assertGreaterEqual(len(tqc.get_instructions("ecr")), 1) for instr in tqc.get_instructions("ecr"): self.assertEqual((0, 1), (tqc_index[instr.qubits[0]], tqc_index[instr.qubits[1]]))
def test_multiple_gate_error_matrix(self): """Test error matrix ona small target with multiple gets on each qubit generates""" target = Target(num_qubits=3) phi = Parameter("phi") lam = Parameter("lam") theta = Parameter("theta") target.add_instruction( RZGate(phi), {(i, ): InstructionProperties(duration=0, error=0) for i in range(3)}) target.add_instruction( UGate(theta, phi, lam), {(i, ): InstructionProperties(duration=1e-7, error=1e-2) for i in range(3)}, ) cx_props = { (0, 1): InstructionProperties(error=1e-3), (0, 2): InstructionProperties(error=1e-3), (1, 0): InstructionProperties(error=1e-3), (1, 2): InstructionProperties(error=1e-3), (2, 0): InstructionProperties(error=1e-3), (2, 1): InstructionProperties(error=1e-3), } target.add_instruction(CXGate(), cx_props) ecr_props = { (0, 1): InstructionProperties(error=2e-2), (1, 2): InstructionProperties(error=2e-2), (2, 0): InstructionProperties(error=2e-2), } target.add_instruction(ECRGate(), ecr_props) expected_error_matrix = np.array([ [1e-2, 2e-2, 1e-3], [1e-3, 1e-2, 2e-2], [2e-2, 1e-3, 1e-2], ]) np.testing.assert_array_equal(expected_error_matrix, DenseLayout(target=target).error_mat)
def setUp(self): super().setUp() self.fake_backend = FakeBackendV2() self.fake_backend_target = self.fake_backend.target self.theta = Parameter("theta") self.phi = Parameter("phi") self.ibm_target = Target() i_props = { (0, ): InstructionProperties(duration=35.5e-9, error=0.000413), (1, ): InstructionProperties(duration=35.5e-9, error=0.000502), (2, ): InstructionProperties(duration=35.5e-9, error=0.0004003), (3, ): InstructionProperties(duration=35.5e-9, error=0.000614), (4, ): InstructionProperties(duration=35.5e-9, error=0.006149), } self.ibm_target.add_instruction(IGate(), i_props) rz_props = { (0, ): InstructionProperties(duration=0, error=0), (1, ): InstructionProperties(duration=0, error=0), (2, ): InstructionProperties(duration=0, error=0), (3, ): InstructionProperties(duration=0, error=0), (4, ): InstructionProperties(duration=0, error=0), } self.ibm_target.add_instruction(RZGate(self.theta), rz_props) sx_props = { (0, ): InstructionProperties(duration=35.5e-9, error=0.000413), (1, ): InstructionProperties(duration=35.5e-9, error=0.000502), (2, ): InstructionProperties(duration=35.5e-9, error=0.0004003), (3, ): InstructionProperties(duration=35.5e-9, error=0.000614), (4, ): InstructionProperties(duration=35.5e-9, error=0.006149), } self.ibm_target.add_instruction(SXGate(), sx_props) x_props = { (0, ): InstructionProperties(duration=35.5e-9, error=0.000413), (1, ): InstructionProperties(duration=35.5e-9, error=0.000502), (2, ): InstructionProperties(duration=35.5e-9, error=0.0004003), (3, ): InstructionProperties(duration=35.5e-9, error=0.000614), (4, ): InstructionProperties(duration=35.5e-9, error=0.006149), } self.ibm_target.add_instruction(XGate(), x_props) cx_props = { (3, 4): InstructionProperties(duration=270.22e-9, error=0.00713), (4, 3): InstructionProperties(duration=305.77e-9, error=0.00713), (3, 1): InstructionProperties(duration=462.22e-9, error=0.00929), (1, 3): InstructionProperties(duration=497.77e-9, error=0.00929), (1, 2): InstructionProperties(duration=227.55e-9, error=0.00659), (2, 1): InstructionProperties(duration=263.11e-9, error=0.00659), (0, 1): InstructionProperties(duration=519.11e-9, error=0.01201), (1, 0): InstructionProperties(duration=554.66e-9, error=0.01201), } self.ibm_target.add_instruction(CXGate(), cx_props) measure_props = { (0, ): InstructionProperties(duration=5.813e-6, error=0.0751), (1, ): InstructionProperties(duration=5.813e-6, error=0.0225), (2, ): InstructionProperties(duration=5.813e-6, error=0.0146), (3, ): InstructionProperties(duration=5.813e-6, error=0.0215), (4, ): InstructionProperties(duration=5.813e-6, error=0.0333), } self.ibm_target.add_instruction(Measure(), measure_props) self.aqt_target = Target(description="AQT Target") rx_props = { (0, ): None, (1, ): None, (2, ): None, (3, ): None, (4, ): None, } self.aqt_target.add_instruction(RXGate(self.theta), rx_props) ry_props = { (0, ): None, (1, ): None, (2, ): None, (3, ): None, (4, ): None, } self.aqt_target.add_instruction(RYGate(self.theta), ry_props) rz_props = { (0, ): None, (1, ): None, (2, ): None, (3, ): None, (4, ): None, } self.aqt_target.add_instruction(RZGate(self.theta), rz_props) r_props = { (0, ): None, (1, ): None, (2, ): None, (3, ): None, (4, ): None, } self.aqt_target.add_instruction(RGate(self.theta, self.phi), r_props) rxx_props = { (0, 1): None, (0, 2): None, (0, 3): None, (0, 4): None, (1, 0): None, (2, 0): None, (3, 0): None, (4, 0): None, (1, 2): None, (1, 3): None, (1, 4): None, (2, 1): None, (3, 1): None, (4, 1): None, (2, 3): None, (2, 4): None, (3, 2): None, (4, 2): None, (3, 4): None, (4, 3): None, } self.aqt_target.add_instruction(RXXGate(self.theta), rxx_props) measure_props = { (0, ): None, (1, ): None, (2, ): None, (3, ): None, (4, ): None, } self.aqt_target.add_instruction(Measure(), measure_props) self.empty_target = Target() self.ideal_sim_target = Target(num_qubits=3, description="Ideal Simulator") self.lam = Parameter("lam") for inst in [ UGate(self.theta, self.phi, self.lam), RXGate(self.theta), RYGate(self.theta), RZGate(self.theta), CXGate(), ECRGate(), CCXGate(), Measure(), ]: self.ideal_sim_target.add_instruction(inst, {None: None})
def test_controlled_u(self): """Test the creation of a controlled U gate.""" theta, phi, lamb = 0.1, 0.2, 0.3 self.assertEqual( UGate(theta, phi, lamb).control(), CUGate(theta, phi, lamb, 0))
def _off_diag_circ(self, theta: float = 1) -> QuantumCircuit: """Circuit implementing the matrix consisting of entries in the off diagonals. Args: theta: Scale factor for the off diagonal entries (e.g. evolution_time/trotter_steps). Returns: The quantum circuit implementing the matrix consisting of entries in the off diagonals. """ theta *= self.off_diag qr = QuantumRegister(self.num_state_qubits) if self.num_state_qubits > 1: qr_ancilla = AncillaRegister(max(1, self.num_state_qubits - 2)) qc = QuantumCircuit(qr, qr_ancilla, name="off_diags") else: qc = QuantumCircuit(qr, name="off_diags") qr_ancilla = None qc.u(-2 * theta, 3 * np.pi / 2, np.pi / 2, qr[0]) for i in range(0, self.num_state_qubits - 1): q_controls = [] qc.cx(qr[i], qr[i + 1]) q_controls.append(qr[i + 1]) # Now we want controlled by 0 qc.x(qr[i]) for j in range(i, 0, -1): qc.cx(qr[i], qr[j - 1]) q_controls.append(qr[j - 1]) qc.x(qr[i]) # Multicontrolled rotation if len(q_controls) > 1: ugate = UGate(-2 * theta, 3 * np.pi / 2, np.pi / 2) qc.append( MCMTVChain(ugate, len(q_controls), 1), q_controls[:] + [qr[i]] + qr_ancilla[:len(q_controls) - 1], ) else: qc.cu(-2 * theta, 3 * np.pi / 2, np.pi / 2, 0, q_controls[0], qr[i]) # Uncompute qc.x(qr[i]) for j in range(0, i): qc.cx(qr[i], qr[j]) qc.x(qr[i]) qc.cx(qr[i], qr[i + 1]) # pylint: disable=unused-argument def control(num_ctrl_qubits=1, label=None, ctrl_state=None): qr_state = QuantumRegister(self.num_state_qubits + 1) if self.num_state_qubits > 1: qr_ancilla = AncillaRegister(max(1, self.num_state_qubits - 1)) qc_control = QuantumCircuit(qr_state, qr_ancilla, name="off_diags") else: qc_control = QuantumCircuit(qr_state, name="off_diags") qr_ancilla = None # Control will be qr[0] q_control = qr_state[0] qr = qr_state[1:] qc_control.cu(-2 * theta, 3 * np.pi / 2, np.pi / 2, 0, q_control, qr[0]) for i in range(0, self.num_state_qubits - 1): q_controls = [] q_controls.append(q_control) qc_control.cx(qr[i], qr[i + 1]) q_controls.append(qr[i + 1]) # Now we want controlled by 0 qc_control.x(qr[i]) for j in range(i, 0, -1): qc_control.cx(qr[i], qr[j - 1]) q_controls.append(qr[j - 1]) qc_control.x(qr[i]) # Multicontrolled x rotation if len(q_controls) > 1: ugate = UGate(-2 * theta, 3 * np.pi / 2, np.pi / 2) qc_control.append( MCMTVChain(ugate, len(q_controls), 1), q_controls[:] + [qr[i]] + qr_ancilla[:len(q_controls) - 1], ) else: qc_control.cu(-2 * theta, 3 * np.pi / 2, np.pi / 2, 0, q_controls[0], qr[i]) # Uncompute qc_control.x(qr[i]) for j in range(0, i): qc_control.cx(qr[i], qr[j]) qc_control.x(qr[i]) qc_control.cx(qr[i], qr[i + 1]) return qc_control qc.control = control return qc