2020#include <stdio.h>
2121#include <string.h>
2222
23+ QkTarget * create_sample_target (bool std_inst );
2324/**
2425 * Test empty constructor for Target
2526 */
@@ -552,6 +553,89 @@ static int test_target_update_instruction(void) {
552553 return result ;
553554}
554555
556+ /**
557+ * Test if our target is compatible with certain instructions.
558+ */
559+ int test_target_instruction_supported (void ) {
560+ QkTarget * sample_target = create_sample_target (true);
561+ int result = Ok ;
562+
563+ char * gate_names [7 ] = {"x" , "y" , "id" , "rz" , "sx" , "reset" };
564+ QkParam * rz_params [1 ] = {
565+ qk_param_from_double (3.14 ),
566+ };
567+ for (uint32_t qubit = 0 ; qubit < 5 ; qubit ++ ) {
568+ uint32_t qargs [1 ] = {qubit };
569+ bool should_be_true = qubit < 4 ;
570+
571+ for (int gate = 0 ; gate < 6 ; gate ++ ) {
572+ // If i == 4 condition should be false unless we try with the y gate
573+ // since y is added as a global gate.
574+ if (qk_target_instruction_supported (sample_target , gate_names [gate ], qargs ,
575+ gate != 3 ? NULL : rz_params ) !=
576+ (should_be_true || gate == 1 )) {
577+ printf ("This target did not correctly demonstrate compatibility with %s and qargs "
578+ "[%d]" ,
579+ gate_names [gate ], qubit );
580+ result = EqualityError ;
581+ goto cleanup ;
582+ }
583+ }
584+
585+ // Try checking with the wrong fixed parameter for RZ
586+ QkParam * param = qk_param_from_double (1.57 );
587+ if (qk_target_instruction_supported (sample_target , "rz" , qargs ,
588+ (QkParam * []){
589+ param ,
590+ })) {
591+ printf ("This target did not correctly demonstrate compatibility with 'rz' and qargs "
592+ "[%d]" ,
593+ qubit );
594+ result = EqualityError ;
595+ qk_param_free (param );
596+ goto cleanup ;
597+ }
598+
599+ // Test standard instructions reset and measure
600+ if (!(qk_target_instruction_supported (sample_target , "measure" , qargs , NULL ) ==
601+ (qubit < 2 ))) {
602+ printf (
603+ "This target did not correctly demonstrate compatibility with 'measure' and qargs "
604+ "[%d]" ,
605+ qubit );
606+ result = EqualityError ;
607+ goto cleanup ;
608+ }
609+ }
610+
611+ // Qarg samples for CX
612+ uint32_t qarg_samples [8 ][2 ] = {
613+ {3 , 4 }, {4 , 3 }, {3 , 1 }, {1 , 3 }, {1 , 2 }, {2 , 1 }, {0 , 1 }, {1 , 0 },
614+ };
615+ for (int i = 0 ; i < 8 ; i ++ ) {
616+ if (!qk_target_instruction_supported (sample_target , "cx" , qarg_samples [i ], NULL )) {
617+ printf ("This target did incorrectly demonstrate compatibility with 'cx' and qargs [%d, "
618+ "%d]" ,
619+ qarg_samples [i ][0 ], qarg_samples [i ][1 ]);
620+ result = EqualityError ;
621+ goto cleanup ;
622+ }
623+ }
624+
625+ uint32_t cx_qargs [2 ] = {3 , 2 };
626+ // Instruction should not show compatibility with (3, 2)
627+ if (qk_target_instruction_supported (sample_target , "cx" , cx_qargs , NULL )) {
628+ printf ("This target did incorrectly demonstrate compatibility with 'cx' and qargs [3, 2]" );
629+ result = EqualityError ;
630+ goto cleanup ;
631+ }
632+
633+ cleanup :
634+ qk_param_free (rz_params [0 ]);
635+ qk_target_free (sample_target );
636+ return result ;
637+ }
638+
555639int test_target (void ) {
556640 int num_failed = 0 ;
557641 num_failed += RUN_TEST (test_empty_target );
@@ -560,9 +644,77 @@ int test_target(void) {
560644 num_failed += RUN_TEST (test_target_add_instruction );
561645 num_failed += RUN_TEST (test_target_update_instruction );
562646 num_failed += RUN_TEST (test_target_construction_ibm_like_target );
647+ num_failed += RUN_TEST (test_target_instruction_supported );
563648
564649 fflush (stderr );
565650 fprintf (stderr , "=== Number of failed subtests: %i\n" , num_failed );
566651
567652 return num_failed ;
568653}
654+
655+ QkTarget * create_sample_target (bool std_inst ) {
656+ // Build sample target
657+ QkTarget * target = qk_target_new (0 );
658+ QkTargetEntry * i_entry = qk_target_entry_new (QkGate_I );
659+ for (int i = 0 ; i < 4 ; i ++ ) {
660+ uint32_t qargs [1 ] = {i };
661+ qk_target_entry_add_property (i_entry , qargs , 1 , 35.5e-9 , 0. );
662+ }
663+ qk_target_add_instruction (target , i_entry );
664+
665+ double rz_params [1 ] = {3.14 };
666+ QkTargetEntry * rz_entry = qk_target_entry_new_fixed (QkGate_RZ , rz_params , NULL );
667+ for (int i = 0 ; i < 4 ; i ++ ) {
668+ uint32_t qargs [1 ] = {i };
669+ qk_target_entry_add_property (rz_entry , qargs , 1 , 0. , 0. );
670+ }
671+ qk_target_add_instruction (target , rz_entry );
672+
673+ QkTargetEntry * sx_entry = qk_target_entry_new (QkGate_SX );
674+ for (int i = 0 ; i < 4 ; i ++ ) {
675+ uint32_t qargs [1 ] = {i };
676+ qk_target_entry_add_property (sx_entry , qargs , 1 , 35.5e-9 , 0. );
677+ }
678+ qk_target_add_instruction (target , sx_entry );
679+
680+ QkTargetEntry * x_entry = qk_target_entry_new (QkGate_X );
681+ for (int i = 0 ; i < 4 ; i ++ ) {
682+ uint32_t qargs [1 ] = {i };
683+ qk_target_entry_add_property (x_entry , qargs , 1 , 35.5e-9 , 0.0005 );
684+ }
685+ qk_target_add_instruction (target , x_entry );
686+
687+ QkTargetEntry * cx_entry = qk_target_entry_new (QkGate_CX );
688+ uint32_t qarg_samples [8 ][2 ] = {
689+ {3 , 4 }, {4 , 3 }, {3 , 1 }, {1 , 3 }, {1 , 2 }, {2 , 1 }, {0 , 1 }, {1 , 0 },
690+ };
691+ double props [8 ][2 ] = {
692+ {2.7022e-11 , 0.00713 }, {3.0577e-11 , 0.00713 }, {4.6222e-11 , 0.00929 }, {4.9777e-11 , 0.00929 },
693+ {2.2755e-11 , 0.00659 }, {2.6311e-11 , 0.00659 }, {5.1911e-11 , 0.01201 }, {5.1911e-11 , 0.01201 },
694+ };
695+ for (int i = 0 ; i < 8 ; i ++ ) {
696+ qk_target_entry_add_property (cx_entry , qarg_samples [i ], 2 , props [i ][0 ], props [i ][1 ]);
697+ }
698+ qk_target_add_instruction (target , cx_entry );
699+
700+ // Add global Y Gate
701+ qk_target_add_instruction (target , qk_target_entry_new (QkGate_Y ));
702+
703+ if (std_inst ) {
704+ QkTargetEntry * meas = qk_target_entry_new_measure ();
705+ for (uint32_t i = 0 ; i < 2 ; i ++ ) {
706+ uint32_t q [1 ] = {i };
707+ qk_target_entry_add_property (meas , q , 1 , 1e-6 , 1e-4 );
708+ }
709+ qk_target_add_instruction (target , meas );
710+
711+ QkTargetEntry * reset = qk_target_entry_new_reset ();
712+ for (uint32_t i = 0 ; i < 4 ; i ++ ) {
713+ uint32_t q [1 ] = {i };
714+ qk_target_entry_add_property (reset , q , 1 , 1e-6 , 1e-4 );
715+ }
716+ qk_target_add_instruction (target , reset );
717+ }
718+
719+ return target ;
720+ }
0 commit comments