def test_mcmt_as_normal_control(self, mcmt_class): """Test that the MCMT can act as normal control gate.""" qc = QuantumCircuit(2) mcmt = mcmt_class(gate=CHGate(), num_ctrl_qubits=1, num_target_qubits=1) qc = qc.compose(mcmt, [0, 1]) ref = QuantumCircuit(2) ref.ch(0, 1) self.assertEqual(qc, ref)
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)), (SwapGate(), CSwapGate()), (HGate(), CHGate()), (U3Gate(0.1, 0.2, 0.3), CU3Gate(0.1, 0.2, 0.3))) @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 test_instruction_init(self): """Test initialization from a circuit.""" gate = CXGate() op = Operator(gate).data target = gate.to_matrix() global_phase_equivalent = matrix_equal(op, target, ignore_phase=True) self.assertTrue(global_phase_equivalent) gate = CHGate() op = Operator(gate).data had = HGate().to_matrix() target = np.kron(had, np.diag([0, 1])) + np.kron(np.eye(2), np.diag([1, 0])) global_phase_equivalent = matrix_equal(op, target, ignore_phase=True) self.assertTrue(global_phase_equivalent)
class TestMCMT(QiskitTestCase): """Test the multi-controlled multi-target circuit.""" @data(MCMT, MCMTVChain) def test_mcmt_label(self, mcmt_class): """Test MCMT label remains functional but is deprecated.""" custom_label = "abc" with self.subTest(msg="init with label and get"): with self.assertWarns(DeprecationWarning): mcmt = mcmt_class( XGate(), num_ctrl_qubits=1, num_target_qubits=1, label=custom_label ) with self.assertWarns(DeprecationWarning): self.assertEqual(mcmt.label, custom_label) with self.subTest(msg="label set and get"): mcmt = mcmt_class(XGate(), num_ctrl_qubits=1, num_target_qubits=1) with self.assertWarns(DeprecationWarning): mcmt.label = custom_label with self.assertWarns(DeprecationWarning): self.assertEqual(mcmt.label, custom_label) with self.subTest(msg="control gate label"): mcmt = mcmt_class(XGate(), num_ctrl_qubits=1, num_target_qubits=1) c_mcmt = mcmt.control() with self.assertWarns(DeprecationWarning): c_mcmt = mcmt.control(label=custom_label) with self.assertWarns(DeprecationWarning): self.assertEqual(c_mcmt.label, custom_label) @data(MCMT, MCMTVChain) def test_mcmt_as_normal_control(self, mcmt_class): """Test that the MCMT can act as normal control gate.""" qc = QuantumCircuit(2) mcmt = mcmt_class(gate=CHGate(), num_ctrl_qubits=1, num_target_qubits=1) qc = qc.compose(mcmt, [0, 1]) ref = QuantumCircuit(2) ref.ch(0, 1) self.assertEqual(qc, ref) def test_missing_qubits(self): """Test that an error is raised if qubits are missing.""" with self.subTest(msg="no control qubits"): with self.assertRaises(AttributeError): _ = MCMT(XGate(), num_ctrl_qubits=0, num_target_qubits=1) with self.subTest(msg="no target qubits"): with self.assertRaises(AttributeError): _ = MCMT(ZGate(), num_ctrl_qubits=4, num_target_qubits=0) def test_different_gate_types(self): """Test the different supported input types for the target gate.""" x_circ = QuantumCircuit(1) x_circ.x(0) for input_gate in [x_circ, QuantumCircuit.cx, QuantumCircuit.x, "cx", "x", CXGate()]: with self.subTest(input_gate=input_gate): mcmt = MCMT(input_gate, 2, 2) if isinstance(input_gate, QuantumCircuit): self.assertEqual(mcmt.gate.definition[0][0], XGate()) self.assertEqual(len(mcmt.gate.definition), 1) else: self.assertEqual(mcmt.gate, XGate()) def test_mcmt_v_chain_ancilla_test(self): """Test too few and too many ancillas for the MCMT V-chain mode.""" with self.subTest(msg="insufficient number of auxiliary qubits on gate"): qc = QuantumCircuit(5) mcmt = MCMTVChain(ZGate(), 3, 1) with self.assertRaises(QiskitError): qc.append(mcmt, range(5)) with self.subTest(msg="too many auxiliary qubits on gate"): qc = QuantumCircuit(9) mcmt = MCMTVChain(ZGate(), 3, 1) with self.assertRaises(QiskitError): qc.append(mcmt, range(9)) @data( [CZGate(), 1, 1], [CHGate(), 1, 1], [CZGate(), 3, 3], [CHGate(), 3, 3], [CZGate(), 1, 5], [CHGate(), 1, 5], [CZGate(), 5, 1], [CHGate(), 5, 1], ) @unpack def test_mcmt_v_chain_simulation(self, cgate, num_controls, num_targets): """Test the MCMT V-chain implementation test on a simulation.""" controls = QuantumRegister(num_controls) targets = QuantumRegister(num_targets) subsets = [tuple(range(i)) for i in range(num_controls + 1)] for subset in subsets: qc = QuantumCircuit(targets, controls) # Initialize all targets to 1, just to be sure that # the generic gate has some effect (f.e. Z gate has no effect # on a 0 state) qc.x(targets) num_ancillas = max(0, num_controls - 1) if num_ancillas > 0: ancillas = QuantumRegister(num_ancillas) qc.add_register(ancillas) qubits = controls[:] + targets[:] + ancillas[:] else: qubits = controls[:] + targets[:] for i in subset: qc.x(controls[i]) mcmt = MCMTVChain(cgate, num_controls, num_targets) qc.compose(mcmt, qubits, inplace=True) for i in subset: qc.x(controls[i]) vec = Statevector.from_label("0" * qc.num_qubits).evolve(qc) # target register is initially |11...1>, with length equal to 2**(n_targets) vec_exp = np.array([0] * (2 ** (num_targets) - 1) + [1]) if isinstance(cgate, CZGate): # Z gate flips the last qubit only if it's applied an odd number of times if len(subset) == num_controls and (num_controls % 2) == 1: vec_exp[-1] = -1 elif isinstance(cgate, CHGate): # if all the control qubits have been activated, # we repeatedly apply the kronecker product of the Hadamard # with itself and then multiply the results for the original # state of the target qubits if len(subset) == num_controls: h_i = 1 / np.sqrt(2) * np.array([[1, 1], [1, -1]]) h_tot = np.array([1]) for _ in range(num_targets): h_tot = np.kron(h_tot, h_i) vec_exp = np.dot(h_tot, vec_exp) else: raise ValueError(f"Test not implement for gate: {cgate}") # append the remaining part of the state vec_exp = np.concatenate( (vec_exp, [0] * (2 ** (num_controls + num_ancillas + num_targets) - vec_exp.size)) ) f_i = state_fidelity(vec, vec_exp) self.assertAlmostEqual(f_i, 1)
'Z' : ZGate(), 'S' : SGate(), 'T' : TGate(), 'T_dg' : TdgGate(), 'S_dg' : SdgGate(), 'Ry' : RYGate(np.pi / 4) } CONTROLLED_GATE_DICT = { 'CX0' : CXGate(), 'CX1' : CXGate(), 'CY0' : CYGate(), 'CY1' : CYGate(), 'CZ0' : CZGate(), 'CZ1' : CYGate(), 'CH0' : CHGate(), 'CH1' : CHGate() } def _state_to_gates(state): gates = [] for qtop_gate, qbot_gate in zip(state[0], state[1]): if qtop_gate in SINGLE_GATE_DICT.keys(): gates.append([qtop_gate, [0]]) gates.append([qbot_gate, [1]]) elif qtop_gate in CONTROLLED_GATE_DICT.keys(): gates.append([qtop_gate, [0, 1] if qtop_gate[-1] == '0' else [1, 0] ]) return gates
def test_controlled_h(self): """Test the creation of a controlled H gate.""" self.assertEqual(HGate().control(), CHGate())